MediaSource.duration
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
MediaSource
接口的属性 duration
用来获取或者设置当前媒体展示的时长。
值
以秒为单位的双精度浮点数。
异常
设置新的值的时候可能会有下面的错误抛出。
InvalidAccessError
DOMException
-
时长尝试设置一个负数,或者为
NaN
则抛出。 InvalidStateError
DOMException
-
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 |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
duration |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- Partial support
- Partial support
- No support
- No support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.