音频 blob 的 URL.createObjectURL 在 Firefox 中给出 TypeError

2024-06-20

我正在尝试从创建的音频 blob 创建对象 URLgetUserMedia。该代码在 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.

Code:

  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;
    }
}

from 如何根据浏览器选择`window.URL.createObjectURL()`和`window.webkitURL.createObjectURL()` https://stackoverflow.com/questions/11277677/javascript-newbie-how-to-choose-between-window-url-createobjecturl-and-window,但这没有用


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

audio_player.mozSrcObject = audio_blob;

您应该考虑使用适配器.js https://github.com/GoogleChrome/webrtc/tree/master/samples/web/js文件来解释浏览器差异。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

音频 blob 的 URL.createObjectURL 在 Firefox 中给出 TypeError 的相关文章

随机推荐