【问题标题】:URL.createObjectURL for audio blob gives TypeError in Firefox音频 blob 的 URL.createObjectURL 在 Firefox 中给出 TypeError
【发布时间】:2014-08-25 07:43:49
【问题描述】:

我正在尝试从getUserMedia 创建的音频 blob 创建一个对象 URL。该代码在 Chrome 中运行,但在 Firefox 中存在问题。

错误:

当我打电话给stopAudioRecorder() 时,它停在audio_player.src = URL.createObjectURL(audio_blob);

TypeError: Argument 1 is not valid for any of the 2-argument overloads of URL.createObjectURL.

代码:

  var stopAudioRecorder = function(audio_recorder) {
    var audio_blob, audio_player, new_recording, save_button;

    audio_recorder.stopRecording();
    audio_blob = audio_recorder.getBlob();

    audio_player = document.createElement("audio");
    audio_player.src = URL.createObjectURL(audio_blob);
    audio_player.controls = true;
    audio_player.play();

    $('#new_recording').appendChild(audio_player);

    recording = false;
    return ($("#record_button")).text("Start recording");
  };

我试图通过添加包装函数来提供一些跨浏览器兼容性

function createObjectURL ( file ) {
    if ( window.webkitURL ) {
        return window.webkitURL.createObjectURL( file );
    } else if ( window.URL && window.URL.createObjectURL ) {
        return window.URL.createObjectURL( file );
    } else {
        return null;
    }
}

来自How to choose between `window.URL.createObjectURL()` and `window.webkitURL.createObjectURL()` based on browser,但这不起作用

【问题讨论】:

  • 试试,audio_player.mozSrcObject = audio_blob;

标签: javascript firefox webrtc getusermedia


【解决方案1】:

在 Firefox 中,您可以直接将 getUserMedia 创建的媒体流赋予音频元素的“mozSrcObject”属性。所以下面的代码应该可以工作:

audio_player.mozSrcObject = audio_blob;

您应该考虑使用adapter.js 文件来解决浏览器的差异。

【讨论】:

  • 如果你需要 URL 怎么办?
  • 对我有用,只需检查浏览器:navigator.userAgent.indexOf('Firefox') != -1
  • 您的链接已损坏
猜你喜欢
  • 2019-07-14
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 2021-12-05
  • 2013-02-05
  • 1970-01-01
  • 2017-12-21
相关资源
最近更新 更多