问题
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