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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
---
title: MediaSource.endOfStream()
slug: Web/API/MediaSource/endOfStream
translation_of: Web/API/MediaSource/endOfStream
---
<div>{{APIRef("Media Source Extensions")}}{{SeeCompatTable}}</div>
<p>{{domxref("MediaSource")}} 接口的 <code><strong>endOfStream()</strong></code> 方法意味着流的结束。</p>
<h2 id="语法">语法</h2>
<pre class="brush: js">mediaSource.endOfStream(endOfStreamError);</pre>
<h3 id="参数">参数</h3>
<dl>
<dt>endOfStreamError {{optional_inline}}</dt>
<dd>一个 {{domxref("DOMString")}},表示当流结束之时需要抛出的异常名。可选的值为:
<ul>
<li><code>network</code>: Terminates playback and signals that a network error has occured. This can be used create a custom error handler related to media streams. For example, you might have a function that handles media chunk requests, separate from other network requests. When you make an <a href="/en-US/docs/Web/API/XMLHttpRequest">XMLHttpRequest</a> call for a media chunk, and <code>onabort</code> or <code>onerror</code> triggers, you might want to call <code>endOfStream('network')</code>, display a descriptive message in the UI, and maybe retry the network request immediately or wait until the network is back up (via some kind of polling.)</li>
<li><code>decode</code>: Terminates playback and signals that a decoding error has occured. This can be used to indicate that a parsing error has occured while fetching media data; maybe the data is corrupt, or is encoded using a codec that the browser doesn't know how to decode.</li>
</ul>
</dd>
</dl>
<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>InvalidStateError</code></td>
<td>{{domxref("MediaSource.readyState")}} is not equal to <code>open</code>, or one or more of the {{domxref("SourceBuffer")}} objects in {{domxref("MediaSource.sourceBuffers")}} are being updated (i.e. their {{domxref("SourceBuffer.updating")}} property is <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">var assetURL = 'frag_bunny.mp4';
// Need to be specific for Blink regarding codecs
// ./mp4info frag_bunny.mp4 | grep Codec
var mimeCodec = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';
<code class="language-js"><span class="keyword token">if</span> <span class="punctuation token">(</span><span class="string token">'MediaSource'</span> <span class="keyword token">in</span> window <span class="operator token">&&</span> MediaSource<span class="punctuation token">.</span><span class="function token">isTypeSupported<span class="punctuation token">(</span></span>mimeCodec<span class="punctuation token">)</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
<span class="keyword token">var</span> mediaSource <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">MediaSource</span><span class="punctuation token">;</span>
<span class="comment token"> //console.log(mediaSource.readyState); // closed
</span> video<span class="punctuation token">.</span>src <span class="operator token">=</span> URL<span class="punctuation token">.</span><span class="function token">createObjectURL<span class="punctuation token">(</span></span>mediaSource<span class="punctuation token">)</span><span class="punctuation token">;</span>
mediaSource<span class="punctuation token">.</span><span class="function token">addEventListener<span class="punctuation token">(</span></span><span class="string token">'sourceopen'</span><span class="punctuation token">,</span> sourceOpen<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span> <span class="keyword token">else</span> <span class="punctuation token">{</span>
console<span class="punctuation token">.</span><span class="function token">error<span class="punctuation token">(</span></span><span class="string token">'Unsupported MIME type or codec: '</span><span class="punctuation token">,</span> mimeCodec<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span>
<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">;</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-endOfStream-void-EndOfStreamError-error', 'endOfStream()')}}</td>
<td>{{Spec2('Media Source Extensions')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<p>{{Compat("api.MediaSource.endOfStream")}}</p>
</div>
<h2 id="参见">参见</h2>
<ul>
<li>{{domxref("SourceBuffer")}}</li>
<li>{{domxref("SourceBufferList")}}</li>
</ul>
|