我正在尝试使用第一个演示MediaSource试用,但我无法播放视频。我使用的来源是一个webm视频" big-buck-bunny_trailer.webm"。我一直得到" Uncaught InvalidStateError:无法执行' endOfStream' on' MediaSource':'更新'属性在此MediaSource的一个或多个SourceBuffers上为true。"。我也处理了sourcebuffer更新标志,但它似乎无法正常工作。
window.URL = window.URL || window.webkitURL;
window.MediaSource = window.MediaSource || window.WebKitMediaSource;
if (!!!window.MediaSource) {
//handle fallback
alert('MediaSource API is not available');
}
var mediaSource = new MediaSource();
var sourceBuffer;
var audio;
var playRange = 0;
var queue = [];
var playing = false;
var contentLength = undefined;
function ajaxMediaGET(url, range, async, callback) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.responseType = "arraybuffer";
xhr.setRequestHeader("Range", range);
xhr.addEventListener('load', onLoad, false);
xhr.send();
function onLoad() {
playRange += Number(xhr.getResponseHeader("Content-Length"));
if(!contentLength) {
var contentLengthString = xhr.getResponseHeader("Content-Range").split("/");
contentLength = Number(contentLengthString[1]);
}
//callback(new Uint8Array(xhr.response));
sourceBuffer.addEventListener("update", function() {
if(!sourceBuffer.updating && queue.length > 0) {// && mediaSource.readyState == "open"
sourceBuffer.appendBuffer(queue.shift());
sourceBuffer.removeEventListener("update");
}
}, false);
if (!sourceBuffer.updating) {
sourceBuffer.appendBuffer(new Uint8Array(xhr.response));
} else {
queue.push(new Uint8Array(xhr.response));
}
if(playRange < contentLength - 1) {
ajaxMediaGET("path-to-server", loadNextRange(playRange), true, undefined);
} else {
mediaSource.endOfStream();
}
}
}
function mediaSourceOpen(e) {
sourceBuffer = e.target.addSourceBuffer('video/webm;codecs="vp8,vorbis"');
ajaxMediaGET("path-to-server", loadNextRange(playRange), true, undefined);
/*if(!playing) {
audio.play();
playing = true;
}*/
}
function initPlayer() {
audio = document.querySelector('video');
audio.src = window.URL.createObjectURL(mediaSource);
mediaSource.addEventListener('sourceopen', mediaSourceOpen, false);
mediaSource.addEventListener('webkitsourceopen', mediaSourceOpen, false);
}
initPlayer();
答案 0 :(得分:-1)
您需要等到sourceBuffer完成更新。替换mediaSource.endOfStream();与
FacesMessage.SEVERITY_ERROR,
查看文档和示例代码 MediaSource.endOfStream()