URL.createObjectURL for audio blob gives TypeError in Firefox

自古美人都是妖i 提交于 2019-12-12 17:25:54

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/25481216/url-createobjecturl-for-audio-blob-gives-typeerror-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!