我目前正在尝试通过XMLHttpRequest在浏览器中预加载MP3文件并在音频播放器中播放。直接在播放器中设置时播放音频文件。因此,在处理响应时我必须出错。
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();
}
我收到以下错误消息:
DEMUXER_ERROR_COULD_NOT_OPEN:FFmpegDemuxer:打开上下文失败(代码4)
有人知道在哪里找到错误吗? 谢谢您的回答。
答案 0 :(得分:0)
尝试一下:
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;
答案 1 :(得分:0)
您可以尝试将ajax请求中的响应类型设置为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();
}