音频blob的URL.createObjectURL在Firefox中给出TypeError [英] URL.createObjectURL for audio blob gives TypeError in Firefox

查看:2177
本文介绍了音频blob的URL.createObjectURL在Firefox中给出TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



错误:

code> stopAudioRecorder()它停在 audio_player.src = URL.createObjectURL(audio_blob);



TypeError:参数1不适用于URL.createObjectURL的任何2重载重载。




$ pre $ 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);
};

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

  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 ,但这并不起作用。在Firefox中,你可以直接给媒体由getUserMedia创建的音频元素的mozSrcObject属性。所以下面的代码应该可以工作:

  audio_player.mozSrcObject = audio_blob; 

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


I'm trying to create a Object URL from an audio blob created from getUserMedia. The code works within Chrome, but there are problems in Firefox.

The error:

When I call stopAudioRecorder() it stops at 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");
  };

I attempted to provide some cross-browser compatibility by adding a wrapper function

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 How to choose between `window.URL.createObjectURL()` and `window.webkitURL.createObjectURL()` based on browser, but that didn't work

解决方案

In Firefox you can directly give the media stream created by getUserMedia to the "mozSrcObject" attribute of the audio element. So the following code should work:

audio_player.mozSrcObject = audio_blob;

You should consider using the adapter.js file to account for browser differences.

这篇关于音频blob的URL.createObjectURL在Firefox中给出TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆