aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/media/autoplay_guide/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/media/autoplay_guide/index.html')
-rw-r--r--files/zh-cn/web/media/autoplay_guide/index.html265
1 files changed, 265 insertions, 0 deletions
diff --git a/files/zh-cn/web/media/autoplay_guide/index.html b/files/zh-cn/web/media/autoplay_guide/index.html
new file mode 100644
index 0000000000..db2ac88dc0
--- /dev/null
+++ b/files/zh-cn/web/media/autoplay_guide/index.html
@@ -0,0 +1,265 @@
+---
+title: Autoplay guide for media and Web Audio APIs
+slug: Web/媒体/Autoplay_guide
+translation_of: Web/Media/Autoplay_guide
+---
+<p>Automatically starting the playback of audio (or videos with audio tracks) immediately upon page load can be an unwelcome surprise to users. While autoplay of media serves a useful purpose, it should be used carefully and only when needed. In order to give users control over this, browsers often provide various forms of autoplay blocking. <span class="seoSummary">In this guide, we'll cover autoplay functionality in the various media and Web Audio APIs, including a brief overview of how to use autoplay and how to work with browsers to handle autoplay blocking gracefully.</span></p>
+
+<p>Autoplay blocking is <em>not</em> applied to {{HTMLElement("video")}} elements when the source media does not have an audio track, or if the audio track is muted. Media with an active audio track are considered to be <strong>audible</strong>, and autoplay blocking applies to them. <strong>Inaudible</strong> media are not affected by autoplay blocking.</p>
+
+<h2 id="自动播放_和_自动播放暂停">自动播放 和 自动播放暂停</h2>
+
+<p>The term <strong>autoplay</strong> refers to any feature that causes audio to begin to play without the user specifically requesting that playback begin. This includes both the use of HTML attributes to autoplay media as well as the user of JavaScript code to start playback outside the context of handling user input.</p>
+
+<p>That means that both of the following are considered autoplay behavior, and are therefore subject to the browser's autoplay blocking policy:</p>
+
+<pre class="brush: html notranslate">&lt;audio src="/music.mp4" autoplay&gt;</pre>
+
+<p>和</p>
+
+<pre class="brush: js notranslate">audioElement.play();
+</pre>
+
+<p dir="ltr" id="tw-target-text">以下网络功能和API可能会受到自动播放阻止的影响:</p>
+
+<ul>
+ <li>The {{Glossary("HTML")}} {{HTMLElement("audio")}} and {{HTMLElement("video")}} elements</li>
+ <li>The <a href="/en-US/docs/Web/API/Web_Audio_API">Web Audio API</a></li>
+</ul>
+
+<p>从用户的角度来看,网页或应用程序自动地发出噪音而没有警告可能会令人讨厌、不便或令人反感。因此,浏览器通常仅允许在特定情况下进行自动播放。</p>
+
+<h3 id="Autoplay功能">Autoplay功能</h3>
+
+<p>据新政策,媒体内容将在满足以下至少一个的条件下自动播放:</p>
+
+<ul>
+ <li>音频被静音或其音量设置为0</li>
+ <li>用户和网页已有交互行为(包括点击、触摸、按下某个键等等)</li>
+ <li>网站已被列入白名单;如果浏览器确定用户经常与媒体互动,这可能会自动发生,也可能通过首选项或其他用户界面功能手动发生</li>
+ <li>自动播放策略应用到{{HTMLElement("iframe")}}或者其文档上</li>
+</ul>
+
+<p>否则,播放可能会被阻止。导致播放被阻塞的确切情况以及将网站列入白名单的具体方法因浏览器而异,但最好是遵循以上的原则。</p>
+
+<p>详情,请参阅 <a href="https://developers.google.com/web/updates/2017/09/autoplay-policy-changes">Google Chrome</a> 和 <a href="https://webkit.org/blog/7734/auto-play-policy-changes-for-macos/">WebKit</a> 的自动播放政策。</p>
+
+<div class="blockIndicator note">
+<p><strong>注意:</strong> 换句话说,如果在尚无任何用户交互的页面中通过编程方式启动播放,则通常会阻止任何包含音频在内的媒体的播放。</p>
+</div>
+
+<h2 id="能自动播放的媒体元素">能自动播放的媒体元素</h2>
+
+<p>既然我们已经介绍了什么是自动播放以及什么可以阻止自动播放,接下来我们将介绍您的网站或应用程序如何在页面加载时自动播放媒体,如何检测何时自动播放失败,以及当自动播放被浏览器拒绝时的应对技巧。</p>
+
+<h3 id="autoplay_属性">autoplay 属性</h3>
+
+<p>想让内容自动播放的最简单方法是将{{htmlattrxref("autoplay", "audio")}}属性添加到{{HTMLElement("audio")}}或{{HTMLElement("video")}}元素。并将{{domxref("HTMLMediaElement.autoplay", "autoplay")}}属性设置为 <code>true</code> ,当 <code>autoplay</code> 的属性为 <code>true</code> 时,媒体元素将在发生以下情况后尽快自动开始播放:</p>
+
+<ul>
+ <li>
+ <p>页面允许使用自动播放功能</p>
+ </li>
+ <li>媒体元素已在页面加载期间创建</li>
+ <li>假设网络性能或带宽没有显着变化,且已收到足够的媒体流并已开始播放,继续播放直至媒体结束而不会中断。</li>
+</ul>
+
+<h4 id="例子1_autoplay属性">例子1: autoplay属性</h4>
+
+<p>使用 <code>autoplay</code> 属性的{{HTMLElement("audio")}}元素就像如下:</p>
+
+<pre class="brush: html notranslate">&lt;audio id="musicplayer" autoplay&gt;
+ &lt;source src="/music/chapter1.mp4"
+&lt;/audio&gt;
+</pre>
+
+<h4 id="例子2:检测自动播放失败">例子2:检测自动播放失败</h4>
+
+<p>如果你依靠自动播放功能去做一些重要的事情,或者自动播放失败会以任何方式影响你的应用程序,那你可能会想知道自动播放什么时候没有开始。不幸的是,对于{{htmlattrxref("autoplay", "audio")}}属性,识别自动播放是否成功开始是很棘手的。自动播放失败时<strong>不会触发</strong>任何事件。也没有抛出异常或可以设置回调,甚至在媒体元素上都没有标记来告诉你自动播放是否起作用。你实际能做的就是检查一些值,然后根据这些值猜测自动播放是否起作用。</p>
+
+<p dir="ltr" id="tw-target-text">如果您能够调整查看内容的方向,那么更好的方法是,依靠知道媒体播放已成功开始,而不是在媒体启动失败时知道。您可以通过侦听要在媒体元素上触发的{{event("play")}}事件来轻松实现此目的。</p>
+
+<p>The <code>play</code> event is sent both when the media is resumed after being paused <em>and</em> when autoplay occurs. That means that the first time the <code>play</code> event is fired, you know your media is being started for the first time after the page is opened.</p>
+
+<p>Consider this HTML for a media element:</p>
+
+<pre class="brush: html notranslate">&lt;video src="myvideo.mp4" autoplay onplay=handleFirstPlay(event)"&gt;</pre>
+
+<p>Here we have a {{HTMLElement("video")}} element whose {{htmlattrxref("autoplay", "video")}} attribute is set, with an {{domxref("HTMLMediaElement.onplay", "onplay")}} event handler set up; the event is handled by a function called <code>handleFirstPlay()</code>, which receives as input the <code>play</code> event.</p>
+
+<p><code>handleFirstPlay()</code> looks like this:</p>
+
+<pre class="brush: js notranslate">function handleFirstPlay(event) {
+ let vid = event.target;
+
+ vid.onplay = null;
+
+ // Start whatever you need to do after playback has started
+}</pre>
+
+<p>After getting a reference to the video element from the {{domxref("Event")}} object's {{domxref("Event.target", "target")}}, the element's <code>onplay</code> handler is set to <code>null</code>. This will prevent any future <code>play</code> events from being delivered to the handler. That could happen if the video is paused and resumed by the user or automatically by the browser when the document is in a background tab.</p>
+
+<p>At this point, your site or app can begin whatever it needs to do that relies upon the video having been started up.</p>
+
+<div class="blockIndicator note">
+<p><strong>Note:</strong> This approach doesn't differentiate between autoplay and the user starting playback manually.</p>
+</div>
+
+<h3 id="The_play_method">The play() method</h3>
+
+<p>The term "autoplay" also refers to scenarios in which a script tries to trigger the playback of media that includes audio, outside the context of handling a user input event. This is done by calling the media element's {{domxref("HTMLMediaElement.play", "play()")}} method.</p>
+
+<div class="blockIndicator note">
+<p><strong>Note:</strong> It is strongly recommended that you use the <code>autoplay</code> attribute whenever possible, because support for autoplay preferences are more widespread for the <code>autoplay</code> attribute than for other means of playing media automatically. It also lets the browser take responsibility for starting playback, letting it optimize the timing of that taking place.</p>
+</div>
+
+<h4 id="Example_Playing_video">Example: Playing video</h4>
+
+<p>This simple example plays the first {{HTMLElement("video")}} element found in the document. <code>play()</code> won't let the playback begin unless the document has permission to automatically play media.</p>
+
+<pre class="brush: js notranslate">document.querySelector("video").play();</pre>
+
+<h4 id="Example_Handling_play_failures">Example: Handling play() failures</h4>
+
+<p>It's much easier to detect a failure to autoplay media when you use the {{domxref("HTMLMediaElement.play", "play()")}} method to start it. <code>play()</code> returns a {{jsxref("Promise")}} which is resolved once the media successfully begins to play, and is rejected when playback fails to begin (such as if autoplay is denied). When autoplay fails, you likely will want to offer a way for the user to manually tell the browser to ask the user to grant permission to play media.</p>
+
+<p>You might use code like this to accomplish the job:</p>
+
+<pre class="brush: js notranslate">let startPlayPromise = videoElem.play();
+
+if (startPlayPromise !== undefined) {
+ startPlayPromise.catch(error =&gt; {
+ if (error.name === "NotAllowedError") {
+ showPlayButton(videoElem);
+ } else {
+ // Handle a load or playback error
+ }
+ }).then(() =&gt; {
+ // Start whatever you need to do only after playback
+ // has begun.
+ });
+}
+</pre>
+
+<p>The first thing we do with the result of <code>play()</code> is make sure it's not <code>undefined</code>. We check for this because in earlier versions of the HTML specification, <code>play()</code> didn't return a value. Returning a promise to allow you to determine success or failure of the operation was added more recently. Checking for <code>undefined</code> prevents this code from failing with an error on older versions of web browsers.</p>
+
+<p>We then add a {{jsxref("Promise.catch", "catch()")}} handler to the promise. This looks at the error's {{domxref("DOMException.name", "name")}} to see if it's <code>NotAllowedError</code>. This indicates that playback failed due to a permission issue, such as autoplay being denied. If that's the case, we should present a user interface to let the user manually start playback; that's handled here by a function <code>showPlayButton()</code>.</p>
+
+<p>Any other errors are handled as appropriate.</p>
+
+<p>If the promise returned by <code>play()</code> is resolved without error, the <code>then()</code> clause is run and can begin whatever needs to be done when autoplay has begun.</p>
+
+<h2 id="Autoplay_using_the_Web_Audio_API">Autoplay using the Web Audio API</h2>
+
+<p>In the <a href="/en-US/docs/Web/API/Web_Audio_API">Web Audio API</a>, a web site or app can start playing audio using the <code>start()</code> method on a source node linked to the {{domxref("AudioContext")}}. Doing so outside the context of handling a user input event is subject to autoplay rules.</p>
+
+<p><em>More content will come soon; autoplay blocking is still being worked on at Mozilla. If others have it already, they are welcome to pitch in with this section...</em></p>
+
+<h2 id="The_autoplay_feature_policy">The autoplay feature policy</h2>
+
+<p>In addition to the browser-side management and control over autoplay functionality described above, a web server can also express its willingness to allow autoplay to function. The {{Glossary("HTTP")}} {{HTTPHeader("Feature-Policy")}} header's <code><a href="/en-US/docs/Web/HTTP/Headers/Feature-Policy/autoplay">autoplay</a></code> directive is used to control which domains, if any, can be used to autoplay media. By default, the <code>autoplay</code> feature policy is set to <code>'self'</code> (<em>including the single quote characters</em>), indicating that autoplay is permitted as they're hosted on the same domain as the document.</p>
+
+<p>You can also specify <code>'none'</code> to disable autoplay entirely, <code>'*'</code> to allow autoplay from all domains, or one or more specific origins from which media can be automatically played. These origins are separated by space characters.</p>
+
+<div class="blockIndicator note">
+<p><strong>Note:</strong> The specified feature policy applies to the document and every {{HTMLElement("iframe")}} nested within it, unless those frames include an {{htmlattrxref("allow", "iframe")}}, which sets a new feature policy for that frame and all frames nested within it.</p>
+</div>
+
+<p>When using the {{htmlattrxref("allow", "iframe")}} attribute on an <code>&lt;iframe&gt;</code> to specify a feature policy for that frame and its nested frames, you can also specify the value <code>'src'</code> to allow autoplay of media only from the same domain as that specified by the frame's {{htmlattrxref("src", "iframe")}} attribute.</p>
+
+<h3 id="Example_Allowing_autoplay_only_from_the_documents_domain">Example: Allowing autoplay only from the document's domain</h3>
+
+<p>To use the {{HTTPHeader("Feature-Policy")}} header to only allow media to autoplay from the document's {{Glossary("origin")}}:</p>
+
+<pre class="notranslate">Feature-Policy: autoplay 'self'</pre>
+
+<p>To do the same for an {{HTMLElement("iframe")}}:</p>
+
+<pre class="brush: html notranslate">&lt;iframe src="mediaplayer.html"
+ allow="autoplay 'src'"&gt;
+&lt;/iframe&gt;
+</pre>
+
+<h3 id="Example_Allowing_autoplay_and_fullscreen_mode">Example: Allowing autoplay and fullscreen mode</h3>
+
+<p>Adding <a href="/en-US/docs/Web/API/Fullscreen_API">Fullscreen API</a> permission to the previous example results in a <code>Feature-Policy</code> header like the following if fullscreen access is allowed regardless of the domain; a domain restriction can be added as well as needed.</p>
+
+<pre class="notranslate">Feature-Policy: autoplay 'self'; fullscreen</pre>
+
+<p>The same permissions, grated using the <code>&lt;iframe&gt;</code> element's <code>allow</code> property, look like this:</p>
+
+<pre class="brush: html notranslate">&lt;iframe src="mediaplayer.html"
+ allow="autoplay 'src'; fullscreen"&gt;
+&lt;/iframe&gt;
+</pre>
+
+<h3 id="Example_Allowing_autoplay_from_specific_sources">Example: Allowing autoplay from specific sources</h3>
+
+<p>The <code>Feature-Policy</code> header to allow media to be played from both the document's (or <code>&lt;iframe&gt;</code>'s) own domain and <code>https://example.media</code> looks like this:</p>
+
+<pre class="notranslate">Feature-Policy: autoplay 'self' https://example.media</pre>
+
+<p>An {{HTMLElement("iframe")}} can be written to specify that this autoplay policy should be applied to itself and any child frames would be written thusly:</p>
+
+<pre class="brush: html notranslate">&lt;iframe width="300" height="200"
+ src="mediaplayer.html"
+ allow="autoplay 'src' https://example.media"&gt;
+&lt;/iframe&gt;
+</pre>
+
+<h3 id="Example_Disabling_autoplay">Example: Disabling autoplay</h3>
+
+<p>Setting the <code>autoplay</code> feature policy to <code>'none'</code> disables autoplay entirely for the document or <code>&lt;iframe&gt;</code> and all nested frames. The HTTP header is:</p>
+
+<pre class="notranslate">Feature-Policy: autoplay 'none'</pre>
+
+<p>Using the <code>&lt;iframe&gt;</code>'s <code>allow</code> attribute:</p>
+
+<pre class="brush: html notranslate">&lt;iframe src="mediaplayer.html"
+ allow="autoplay 'none'"&gt;
+&lt;/iframe&gt;
+</pre>
+
+<h2 id="Best_practices">Best practices</h2>
+
+<p>Tips and recommended best practices to help you make the most of working with autoplay are offered here.</p>
+
+<h3 id="Handling_autoplay_failure_with_media_controls">Handling autoplay failure with media controls</h3>
+
+<p>A common use case for autoplay is to automatically begin to play a video clip that goes along with an article, an advertisement, or a preview of the page's main functionality. To autoplay videos like these, you have two options: don't have an audio track, or have an audio track but configure the {{HTMLElement("video")}} element to mute the audio by default, like this:</p>
+
+<pre class="brush: html notranslate">&lt;video src="/videos/awesomevid.webm" controls autoplay muted&gt;</pre>
+
+<p>This video element is configured to include the user controls (typically play/pause, scrubbing through the video's timeline, volume control, and muting); also, since the {{htmlattrxref("muted", "video")}} attribute is included, the video will autoplay but with the audio muted. The user has the option, however, of re-enabling the audio by clicking on the unmute button in the controls.</p>
+
+<h2 id="Browser_configuration_options">Browser configuration options</h2>
+
+<p>Browsers may have preferences that control the way autoplay works, or how autoplay blocking is handled. Here, any such preferences that may be of special significance or importance to you as a web developer are listed. These include any that may aid in testing or debugging as well as any that could be set in a way that you need to be prepared to handle.</p>
+
+<h3 id="Firefox">Firefox</h3>
+
+<dl>
+ <dt><code>media.allowed-to-play.enabled</code></dt>
+ <dd>A Boolean preference which specifies whether or not the {{domxref("HTMLMediaElement.allowedToPlay")}} property is exposed to the web. This is currently <code>false</code> by default (except in nightly builds, where it's <code>true</code> by default). If this is <code>false</code>, the <code>allowedToPlay</code> property is missing from the <code>HTMLMediaElement</code> interface, and is thus not present on either {{HTMLElement("audio")}} or {{HTMLElement("video")}} elements.</dd>
+ <dt><code>media.autoplay.allow-extension-background-pages</code></dt>
+ <dd>This Boolean preference, if <code>true</code>, allows browser extensions' background scripts to autoplay audio media. Setting this value to <code>false</code> disables this capability. The default value is <code>true</code>.</dd>
+ <dt><code>media.autoplay.allow-muted</code></dt>
+ <dd>A Boolean preference which if <code>true</code> (the default) allows audio media which is currently muted to be automatically played. If this has been changed to <code>false</code>, media with an audio track will not be permitted to play even if muted.</dd>
+ <dt><code>media.autoplay.block-webaudio</code></dt>
+ <dd>A Boolean preference which indicates whether or not to apply autoplay blocking to the <a href="/en-US/docs/Web/API/Web_Audio_API">Web Audio API</a>. The default is <code>true</code>.</dd>
+ <dt><code>media.autoplay.default</code></dt>
+ <dd>An integer preference which specifies whether per-domain configuration for autoplay support by default is allowed (<code>0</code>), blocked (<code>1</code>), or prompt-on-use (<code>2</code>). The default value is <code>0</code>.</dd>
+ <dt><code>media.autoplay.enabled.user-gestures-needed</code> (Nightly builds only)</dt>
+ <dd>A Boolean preference which controls whether or not detection of user gestures is allowed to override the setting of <code>media.autoplay.default</code>. If <code>media.autoplay.default</code> is <em>not</em> set to <code>0</code> (autoplay allowed by default), this preference being <code>true</code> allows autoplay of media with audio tracks anyway if the page has been activated by user gestures, and media that isn't audible is not restricted at all.</dd>
+ <dt><code>media.block-autoplay-until-in-foreground</code></dt>
+ <dd>A Boolean preference which indicates whether or not media playback is blocked when started on a background tab. The default value, <code>true</code>, means that even when otherwise available, autoplay won't take place until after a tab is brought to the foreground. This prevents the distracting situation in which a tab begins playing sound and the user can't find the tab among all their tabs and windows.</dd>
+</dl>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li><a href="/en-US/docs/Web/Media">Web media technologies</a></li>
+ <li><a href="/en-US/docs/Learn/HTML/Multimedia_and_embedding/Video_and_audio_content">Video and audio content</a> (Learning guide)</li>
+ <li><a href="/en-US/docs/Web/API/Web_Audio_API/Using_Web_Audio_API">Using the Web Audio API</a></li>
+ <li><a href="/en-US/docs/Web/Apps/Fundamentals/Audio_and_video_delivery/Cross-browser_audio_basics">Cross-browser audio basics</a></li>
+</ul>