MediaSource.duration

This translation is incomplete. Please help translate this article from English

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

MediaSource 接口的属性 duration 用来获取或者设置当前媒体展示的时长.

语法

mediaSource.duration = 5.5; // 5.5 seconds

var myDuration = mediaSource.duration;

以秒为单位的 双精度浮点数.

错误

设置新的值的时候可能会有下面的错误抛出.

错误 异常
InvalidAccessError 时长尝试设置一个负数, 或者 NaN.
InvalidStateError MediaSource.readyState 状态不是 open, 或者 一个或多个 SourceBuffer 对象在 MediaSource.sourceBuffers 中被更新  (例如. 该 SourceBuffer.updating属性 为 true.)

例子

下面的代码片段取自 Nick Desaulniers写的简单例子 (想进一步了解 查看完整例子, 或者 下载源代码 .)

function sourceOpen (_) {
  //console.log(this.readyState); // open
  var mediaSource = this;
  var sourceBuffer = mediaSource.addSourceBuffer(mimeCodec);
  fetchAB(assetURL, function (buf) {
    sourceBuffer.addEventListener('updateend', function (_) {
      mediaSource.endOfStream();
      mediaSource.duration = 120;
      video.play();
      //console.log(mediaSource.readyState); // ended
    });
    sourceBuffer.appendBuffer(buf);
  });
};

...

规范

Specification Status Comment
Media Source Extensions
duration
Recommendation Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
duration
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

相关链接