diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/mediasource | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/mediasource')
-rw-r--r-- | files/zh-cn/web/api/mediasource/addsourcebuffer/index.html | 173 | ||||
-rw-r--r-- | files/zh-cn/web/api/mediasource/duration/index.html | 94 | ||||
-rw-r--r-- | files/zh-cn/web/api/mediasource/endofstream/index.html | 111 | ||||
-rw-r--r-- | files/zh-cn/web/api/mediasource/index.html | 128 | ||||
-rw-r--r-- | files/zh-cn/web/api/mediasource/mediasource/index.html | 54 | ||||
-rw-r--r-- | files/zh-cn/web/api/mediasource/readystate/index.html | 136 |
6 files changed, 696 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/mediasource/addsourcebuffer/index.html b/files/zh-cn/web/api/mediasource/addsourcebuffer/index.html new file mode 100644 index 0000000000..3e2194c7af --- /dev/null +++ b/files/zh-cn/web/api/mediasource/addsourcebuffer/index.html @@ -0,0 +1,173 @@ +--- +title: MediaSource.addSourceBuffer() +slug: Web/API/MediaSource/addSourceBuffer +translation_of: Web/API/MediaSource/addSourceBuffer +--- +<div>{{APIRef("Media Source Extensions")}}{{SeeCompatTable}}</div> + +<p>{{domxref("MediaSource")}} 的 <code><strong>addSourceBuffer()</strong></code> 方法会根据给定的 MIME 类型创建一个新的 {{domxref("SourceBuffer")}} 对象,然后会将它追加到 <code>MediaSource 的</code> {{domxref("SourceBuffers")}} 列表中。</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">var sourceBuffer = mediaSource.addSourceBuffer(mimeType);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>mimeType</dt> + <dd>你想创建的 source buffer 的 MIME 类型。</dd> +</dl> + +<h3 id="返回">返回</h3> + +<p>一个 {{domxref("SourceBuffer")}} 对象。</p> + +<h3 id="错误(_Errors_)">错误( Errors )</h3> + +<p>下面的错误可能会在调用该方法时被抛出。</p> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Error</th> + <th scope="col">Explanation</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>InvalidAccessError</code></td> + <td>提交的 <code>mimeType</code> 是一个空字符串。</td> + </tr> + <tr> + <td><code>InvalidStateError</code></td> + <td>{{domxref("MediaSource.readyState")}} 的值不等于 <code>open</code>.</td> + </tr> + <tr> + <td><code>NotSupportedError</code></td> + <td>当前浏览器不支持你提交的 <code>mimeType</code> , 或者 it is not compatible with the <code>mimeType</code>s specified for the {{domxref("SourceBuffer")}} objects that already exist in {{domxref("MediaSource.sourceBuffers")}}.</td> + </tr> + <tr> + <td><code>QuotaExceededError</code></td> + <td>The user agent can't handle any more SourceBuffer objects, or creating a SourceBuffer based on this <code>mimeType</code> would result in an unsupported <a href="https://w3c.github.io/media-source/#sourcebuffer-configuration">SourceBuffer configuration.</a></td> + </tr> + </tbody> +</table> + +<h2 id="Example">Example</h2> + +<p>The following snippet is from a simple example written by Nick Desaulniers (<a href="http://nickdesaulniers.github.io/netfix/demo/bufferAll.html">view the full demo live</a>, or <a href="https://github.com/nickdesaulniers/netfix/blob/gh-pages/demo/bufferAll.html">download the source</a> for further investigation.)</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="Specifications">Specifications</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-addSourceBuffer-SourceBuffer-DOMString-type', 'addSourceBuffer()')}}</td> + <td>{{Spec2('Media Source Extensions')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Edge</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>23</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("25.0")}}<sup>[1]</sup><br> + {{CompatGeckoDesktop("42.0")}}</td> + <td>11<sup>[2]</sup></td> + <td>15</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Edge</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>4.4.4</td> + <td>{{CompatVersionUnknown}}</td> + <td> + <p>{{CompatNo}}</p> + </td> + <td>{{CompatNo}}</td> + <td>11</td> + <td>30</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<p>[1] Available after switching the <code>about:config</code> preference <code>media.mediasource.enabled</code> to <code>true</code>. In addition, support was limited to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist was removed and Media Source Extensions was enabled by default in 42+ for all sites.</p> + +<p>[2] Only works on Windows 8+.</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{domxref("SourceBuffer")}}</li> + <li>{{domxref("SourceBufferList")}}</li> +</ul> diff --git a/files/zh-cn/web/api/mediasource/duration/index.html b/files/zh-cn/web/api/mediasource/duration/index.html new file mode 100644 index 0000000000..400256f465 --- /dev/null +++ b/files/zh-cn/web/api/mediasource/duration/index.html @@ -0,0 +1,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> diff --git a/files/zh-cn/web/api/mediasource/endofstream/index.html b/files/zh-cn/web/api/mediasource/endofstream/index.html new file mode 100644 index 0000000000..ca5ad6b67d --- /dev/null +++ b/files/zh-cn/web/api/mediasource/endofstream/index.html @@ -0,0 +1,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> diff --git a/files/zh-cn/web/api/mediasource/index.html b/files/zh-cn/web/api/mediasource/index.html new file mode 100644 index 0000000000..bd15ef934d --- /dev/null +++ b/files/zh-cn/web/api/mediasource/index.html @@ -0,0 +1,128 @@ +--- +title: MediaSource +slug: Web/API/MediaSource +translation_of: Web/API/MediaSource +--- +<p>{{APIRef("Media Source Extensions")}}{{SeeCompatTable}}</p> + +<p><strong><code>MediaSource</code></strong>是<a href="/en-US/docs/Web/API/Media_Source_Extensions_API">Media Source Extensions API</a> 表示媒体资源{{domxref("HTMLMediaElement")}}对象的接口。<code>MediaSource</code> 对象可以附着在{{domxref("HTMLMediaElement")}}在客户端进行播放。</p> + +<h2 id="构造函数">构造函数</h2> + +<dl> + <dt>{{domxref("MediaSource.MediaSource", "MediaSource()")}}</dt> + <dd>构造并且返回一个新的<code>MediaSource</code>的空对象(with no associated source buffers)。</dd> +</dl> + +<h2 id="属性">属性</h2> + +<dl> + <dt>{{domxref("MediaSource.sourceBuffers")}} {{readonlyInline}}</dt> + <dd>返回一个{{domxref("SourceBufferList")}} 对象,包含了这个<code>MediaSource的</code>{{domxref("SourceBuffer")}}的对象列表。</dd> + <dt>{{domxref("MediaSource.activeSourceBuffers")}} {{readonlyInline}}</dt> + <dd>返回一个 {{domxref("SourceBufferList")}} 对象,包含了这个{{domxref("MediaSource.sourceBuffers")}}中的{{domxref("SourceBuffer")}} 子集的对象—即提供当前被选中的视频轨 (video track),启用的音频轨 (audio tracks) 以及显示/隐藏的字幕轨 (text tracks) 的对象列表。</dd> + <dt> </dt> + <dt>{{domxref("MediaSource.readyState")}} {{readonlyInline}}</dt> + <dd><code><font face="Open Sans, arial, x-locale-body, sans-serif">返回一个包含当前</font>MediaSource</code>状态的集合,即使它当前没有附着到一个media元素(<code>closed</code>),或者已附着并准备接收{{domxref("SourceBuffer")}} 对象 (<code>open</code>),亦或者已附着但这个流已被{{domxref("MediaSource.endOfStream()")}}关闭(<code>ended</code>.)</dd> + <dt>{{domxref("MediaSource.duration")}}</dt> + <dd>获取和设置当前正在推流媒体的持续时间。</dd> +</dl> + +<dl> +</dl> + +<dl> +</dl> + +<h2 id="方法">方法</h2> + +<p><em>从父接口{{domxref("EventTarget")}}上继承而来。</em></p> + +<dl> + <dt>{{domxref("MediaSource.addSourceBuffer()")}}</dt> + <dd>创建一个带有给定MIME类型的新的 {{domxref("SourceBuffer")}} 并添加到 <code>MediaSource 的 </code>{{domxref("SourceBuffers")}} 列表。</dd> + <dt>{{domxref("MediaSource.removeSourceBuffer()")}}</dt> + <dd>删除指定的{{domxref("SourceBuffer")}} 从这个<code>MediaSource</code>对象中的 {{domxref("SourceBuffers")}}列表。</dd> + <dt>{{domxref("MediaSource.endOfStream()")}}</dt> + <dd>表示流的结束。</dd> + <dt> + <h2 id="静态方法">静态方法</h2> + </dt> + <dt>{{domxref("MediaSource.isTypeSupported()")}}</dt> + <dd>返回一个 {{domxref("Boolean")}} 值表明给定的MIME类型是否被当前的浏览器支持——这意味着是否可以成功的创建这个MIME类型的{{domxref("SourceBuffer")}} 对象。</dd> +</dl> + +<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">var video = document.querySelector('video'); + +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"'; + +if ('MediaSource' in window && MediaSource.isTypeSupported(mimeCodec)) { + var mediaSource = new MediaSource(); + //console.log(mediaSource.readyState); // closed + video.src = URL.createObjectURL(mediaSource); + mediaSource.addEventListener('sourceopen', sourceOpen); +} else { + console.error('Unsupported MIME type or codec: ', mimeCodec); +} + +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(); + video.play(); + //console.log(mediaSource.readyState); // ended + }); + sourceBuffer.appendBuffer(buf); + }); +}; + +function fetchAB (url, cb) { + console.log(url); + var xhr = new XMLHttpRequest; + xhr.open('get', url); + xhr.responseType = 'arraybuffer'; + xhr.onload = function () { + cb(xhr.response); + }; + xhr.send(); +};</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', '#mediasource', 'MediaSource')}}</td> + <td>{{Spec2('Media Source Extensions')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器支持情况">浏览器支持情况</h2> + +<div> +<p>{{Compat("api.MediaSource")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{domxref("SourceBuffer")}}</li> + <li>{{domxref("SourceBufferList")}}</li> +</ul> diff --git a/files/zh-cn/web/api/mediasource/mediasource/index.html b/files/zh-cn/web/api/mediasource/mediasource/index.html new file mode 100644 index 0000000000..a36f3133b8 --- /dev/null +++ b/files/zh-cn/web/api/mediasource/mediasource/index.html @@ -0,0 +1,54 @@ +--- +title: MediaSource.MediaSource() +slug: Web/API/MediaSource/MediaSource +translation_of: Web/API/MediaSource/MediaSource +--- +<div>{{APIRef("Media Source Extensions")}}{{SeeCompatTable}}</div> + +<p><code><strong>MediaSource()</strong></code> 是 {{domxref("MediaSource")}} 的构造函数 返回一个没有分配source buffers 新的 <code>MediaSource</code> 对象.</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">var mediaSource = new MediaSource();</pre> + +<h3 id="参数">参数</h3> + +<p>无.</p> + +<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">var video = document.querySelector('video'); + +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"'; + +if ('MediaSource' in window && MediaSource.isTypeSupported(mimeCodec)) { + var mediaSource = new MediaSource; + //console.log(mediaSource.readyState); // closed + video.src = URL.createObjectURL(mediaSource); + mediaSource.addEventListener('sourceopen', sourceOpen); +} else { + console.error('Unsupported MIME type or codec: ', mimeCodec); +} + +... +</pre> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.MediaSource.MediaSource")}}</p> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{domxref("SourceBuffer")}}</li> + <li>{{domxref("SourceBufferList")}}</li> +</ul> diff --git a/files/zh-cn/web/api/mediasource/readystate/index.html b/files/zh-cn/web/api/mediasource/readystate/index.html new file mode 100644 index 0000000000..6a8831abd0 --- /dev/null +++ b/files/zh-cn/web/api/mediasource/readystate/index.html @@ -0,0 +1,136 @@ +--- +title: MediaSource.readyState +slug: Web/API/MediaSource/readyState +translation_of: Web/API/MediaSource/readyState +--- +<div>{{APIRef("Media Source Extensions")}}{{SeeCompatTable}}</div> + +<p><code><strong>readyState</strong></code>是 {{domxref("MediaSource")}} 接口的一个只读属性。它返回一个集合表明当前<code>MediaSource</code>的状态。它有三种可能的返回值:</p> + +<ul> + <li><code>closed</code>: 当前源并未附着到一个media元素上。</li> + <li><code>open</code>: 当前源已附着到一个media元素并准备好接收 {{domxref("SourceBuffer")}} 对象。</li> + <li><code>ended</code>: 当前源已附着到一个media元素,但流已被{{domxref("MediaSource.endOfStream()")}}结束。</li> +</ul> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">var myReadyState = mediaSource.readyState;</pre> + +<h3 id="值">值</h3> + +<p>A {{domxref("DOMString")}}.</p> + +<h2 id="例子">例子</h2> + +<p>The following snippet is from a simple example written by Nick Desaulniers (<a href="http://nickdesaulniers.github.io/netfix/demo/bufferAll.html">view the full demo live</a>, or <a href="https://github.com/nickdesaulniers/netfix/blob/gh-pages/demo/bufferAll.html">download the source</a> for further investigation.)</p> + +<pre class="brush: js language-js"><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-readyState', 'readyState')}}</td> + <td>{{Spec2('Media Source Extensions')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性表格">浏览器兼容性表格</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Edge</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>23</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("25.0")}}<sup>[1]</sup><br> + {{CompatGeckoDesktop("42.0")}}</td> + <td>11<sup>[2]</sup></td> + <td>15</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Edge</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>4.4.4</td> + <td>{{CompatVersionUnknown}}</td> + <td> + <p>{{CompatNo}}</p> + </td> + <td>{{CompatNo}}</td> + <td>11</td> + <td>30</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<p>[1] Available after switching the <code>about:config</code> preference <code>media.mediasource.enabled</code> to <code>true</code>. In addition, support was limited to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist was removed and Media Source Extensions was enabled by default in 42+ for all sites.</p> + +<p>[2] Only works on Windows 8+.</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{domxref("SourceBuffer")}}</li> + <li>{{domxref("SourceBufferList")}}</li> +</ul> |