aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/mediasource/duration/index.html
blob: 400256f4658d1d5d24beed6545f5e6946404a582 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
---
title: MediaSource.duration
slug: Web/API/MediaSource/duration
translation_of: Web/API/MediaSource/duration
---
<div>{{APIRef("Media Source Extensions")}}{{SeeCompatTable}}</div>

<p>{{domxref("MediaSource")}} 接口的属性 <code><strong>duration</strong></code> 用来获取或者设置当前媒体展示的时长.</p>

<h2 id="语法">语法</h2>

<pre class="brush: js">mediaSource.duration = 5.5; // 5.5 seconds

var myDuration = mediaSource.duration;</pre>

<h3 id="值"></h3>

<p>以秒为单位的 双精度浮点数.</p>

<h3 id="错误">错误</h3>

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

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">错误</th>
   <th scope="col">异常</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>InvalidAccessError</code></td>
   <td>时长尝试设置一个负数, 或者 <code>NaN</code>.</td>
  </tr>
  <tr>
   <td><code>InvalidStateError</code></td>
   <td>{{domxref("MediaSource.readyState")}} 状态不是 <code>open</code>, 或者 一个或多个 {{domxref("SourceBuffer")}} 对象在 {{domxref("MediaSource.sourceBuffers")}} 中被更新  (例如. 该 {{domxref("SourceBuffer.updating")}}属性 为 <code>true</code>.)</td>
  </tr>
 </tbody>
</table>

<h2 id="例子">例子</h2>

<p>下面的代码片段取自 Nick Desaulniers写的简单例子 (想进一步了解 <a href="http://nickdesaulniers.github.io/netfix/demo/bufferAll.html">查看完整例子</a>, 或者 <a href="https://github.com/nickdesaulniers/netfix/blob/gh-pages/demo/bufferAll.html">下载源代码</a> .)</p>

<pre class="brush: js  language-js"><code class="language-js"><span class="keyword token">function</span> sourceOpen <span class="punctuation token">(</span>_<span class="punctuation token">)</span> <span class="punctuation token">{</span>
 <span class="comment token"> //console.log(this.readyState); // open
</span>  <span class="keyword token">var</span> mediaSource <span class="operator token">=</span> <span class="keyword token">this</span><span class="punctuation token">;</span>
  <span class="keyword token">var</span> sourceBuffer <span class="operator token">=</span> mediaSource<span class="punctuation token">.</span><span class="function token">addSourceBuffer<span class="punctuation token">(</span></span>mimeCodec<span class="punctuation token">)</span><span class="punctuation token">;</span>
  <span class="function token">fetchAB<span class="punctuation token">(</span></span>assetURL<span class="punctuation token">,</span> <span class="keyword token">function</span> <span class="punctuation token">(</span>buf<span class="punctuation token">)</span> <span class="punctuation token">{</span>
    sourceBuffer<span class="punctuation token">.</span><span class="function token">addEventListener<span class="punctuation token">(</span></span><span class="string token">'updateend'</span><span class="punctuation token">,</span> <span class="keyword token">function</span> <span class="punctuation token">(</span>_<span class="punctuation token">)</span> <span class="punctuation token">{</span>
      mediaSource<span class="punctuation token">.</span><span class="function token">endOfStream<span class="punctuation token">(</span></span><span class="punctuation token">)</span><span class="punctuation token">;
      mediaSource.duration = 120;</span>
      video<span class="punctuation token">.</span><span class="function token">play<span class="punctuation token">(</span></span><span class="punctuation token">)</span><span class="punctuation token">;</span>
     <span class="comment token"> //console.log(mediaSource.readyState); // ended
</span>    <span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
    sourceBuffer<span class="punctuation token">.</span><span class="function token">appendBuffer<span class="punctuation token">(</span></span>buf<span class="punctuation token">)</span><span class="punctuation token">;</span>
  <span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span><span class="punctuation token">;

...</span></code></pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Media Source Extensions', '#widl-MediaSource-duration', 'duration')}}</td>
   <td>{{Spec2('Media Source Extensions')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>


<p>{{Compat("api.MediaSource.duration")}}</p>
</div>

<h2 id="相关链接">相关链接</h2>

<ul>
 <li>{{domxref("SourceBuffer")}}</li>
 <li>{{domxref("SourceBufferList")}}</li>
</ul>