MediaSource.duration
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
MediaSource 接口的属性 duration 用来获取或者设置当前媒体展示的时长。
值
以秒为单位的双精度浮点数。
异常
设置新的值的时候可能会有下面的错误抛出。
InvalidAccessErrorDOMException-
时长尝试设置一个负数,或者为
NaN则抛出。 InvalidStateErrorDOMException-
MediaSource.readyState的状态不是open,或者有一个或多个在MediaSource.sourceBuffers中的SourceBuffer对象被更新(例如它们的SourceBuffer.updating属性是true),则抛出该错误。
示例
以下片段基于 Nick Desaulniers 编写的一个简单示例(查看完整的在线演示,或者下载源代码进行进一步研究)。
js
function sourceOpen() {
//console.log(this.readyState); // open
const mediaSource = this;
const sourceBuffer = mediaSource.addSourceBuffer(mimeCodec);
fetchAB(assetURL, function (buf) {
sourceBuffer.addEventListener("updateend", () => {
mediaSource.endOfStream();
mediaSource.duration = 120;
video.play();
//console.log(mediaSource.readyState); // ended
});
sourceBuffer.appendBuffer(buf);
});
}
// …
规范
| Specification |
|---|
| Media Source Extensions™> # dom-mediasource-duration> |
浏览器兼容性
Loading…