Reputation: 41
I am currently trying to pre-load an MP3 file via an XMLHttpRequest in the browser and play it in the audio player. The audio file is played when set directly in the player. Therefore, I must have an error when processing the response.
loadAudio : function(playerId, source) {
var player = document.getElementById(playerId);
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
console.log('readyState: ' + request.readyState + ', status: ' + request.status)
if (this.readyState == 4 && this.status == 200) {
console.log(request.getAllResponseHeaders());
var blob = new Blob( [request.response] );
var url = URL.createObjectURL( blob );
player.src = url;
player.addEventListener('loaded', function(e) {
URL.revokeObjectURL(player.src);
});
player.play();
// Typical action to be performed when the document is ready:
}
};
request.open("GET", source, true);
request.send();
}
I receive the following error message:
DEMUXER_ERROR_COULD_NOT_OPEN: FFmpegDemuxer: open context failed (code 4)
Does anyone have any idea where to find the error? Thank you for your answers.
Upvotes: 2
Views: 1782
Reputation: 97672
You can try setting the response type in your ajax request to blob.
loadAudio : function(playerId, source) {
var player = document.getElementById(playerId);
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
console.log('readyState: ' + request.readyState + ', status: ' + request.status)
if (this.readyState == 4 && this.status == 200) {
var url = URL.createObjectURL( request.response);
player.src = url;
player.addEventListener('loaded', function(e) {
URL.revokeObjectURL(player.src);
});
player.play();
// Typical action to be performed when the document is ready:
}
};
request.open("GET", source, true);
request.responseType = 'blob';
request.send();
}
Upvotes: 2
Reputation: 407
Try this:
var blob = new Blob( [ request.response], { type: "audio/mp3" } );
var url = null;
if ( window.webkitURL ) {
url = window.webkitURL.createObjectURL(blob);
} else if ( window.URL && window.URL.createObjectURL ) {
url = window.URL.createObjectURL(blob);
}
player.addEventListener('loaded', function(e) {
URL.revokeObjectURL(url);
});
player.addEventListener('load', function(e) {
URL.revokeObjectURL(url);
});
player.src = url;
Upvotes: 0