diff options
Diffstat (limited to 'files/zh-cn/web/api/mediasource/addsourcebuffer/index.html')
-rw-r--r-- | files/zh-cn/web/api/mediasource/addsourcebuffer/index.html | 173 |
1 files changed, 173 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> |