管理自动播放
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
桌面设备和移动网站设备均支持自动播放功能。
从 Chrome 53 和 iOS 10 开始,Android 和 iPhone 支持内嵌静音自动播放。
Safari 11(桌面版)已更改其处理自动播放的方式
视频。Google
Chrome 也做出了类似的更改
于 2018 年 4 月推出
如果您的网站目前会自动播放视频,请更新
来处理这些新政策新的尝试自动播放代码
示例
演示如何尝试自动播放视频并回退到
点击播放。本指南将为您介绍这个新示例。
在浏览器中检测自动播放是否成功
目前,网络浏览器不提供用于检查自动播放功能是否已启用的简单查询,
是否支持。检查视频是否可以自动播放的唯一方法是
以下代码段演示了此方法:
var contentVideo = document.getElementById('myVideo');
var promise = contentVideo.play();
if (promise !== undefined) {
promise.then(_ => {
// Autoplay worked!
}).catch(error => {
// Autoplay failed.
});
}
代码首先对返回play()
Promise
。在我们的示例中,
Promise
用于检测自动播放功能,并设置
AdsRequest
。
自动播放和 IMA
IMA SDK AdsRequest
有两个与自动播放相关的字段,您需要
供应:
setAdWillAutoPlay
和setAdWillPlayMuted
。
前者可以确保广告服务器仅返回获得
自动播放(如果设为 true),而后者可确保广告服务器
只返回允许在静音或取消静音状态下开始播放的广告。
我们的示例使用 内容视频 作为指示浏览器是否
支持自动播放进行两项检查,了解三种可能的结果:

要进行这些检查,请尝试播放内容视频并查看返回的
Promise
:
var adsInitialized, autoplayAllowed, autoplayRequiresMuted;
// Called on page load.
function init() {
videoContent = document.getElementById('contentElement');
playButton = document.getElementById('playButton');
// Hide the play button unless we need click-to-play.
playButton.style.display = 'none';
// Add an event listener now in case we need to fall back to click-to-play.
playButton.addEventListener('click', () => {
adDisplayContainer.initialize();
adsInitialized = true;
videoContent.load();
playAds();
});
// Create your AdsLoader and AdDisplayContainer here.
setUpIMA();
// Check if autoplay is supported.
checkAutoplaySupport();
}
function checkAutoplaySupport() {
var playPromise = videoContent.play();
if (playPromise !== undefined) {
playPromise.then(onAutoplayWithSoundSuccess).catch(onAutoplayWithSoundFail);
}
}
function onAutoplayWithSoundSuccess() {
// If we make it here, unmuted autoplay works.
videoContent.pause();
autoplayAllowed = true;
autoplayRequiresMuted = false;
autoplayChecksResolved();
}
function onAutoplayWithSoundFail() {
// Unmuted autoplay failed. Now try muted autoplay.
checkMutedAutoplaySupport();
}
function checkMutedAutoplaySupport() {
videoContent.volume = 0;
videoContent.muted = true;
var playPromise = videoContent.play();
if (playPromise !== undefined) {
playPromise.then(onMutedAutoplaySuccess).catch(onMutedAutoplayFail);
}
}
function onMutedAutoplaySuccess() {
// If we make it here, muted autoplay works but unmuted autoplay does not.
videoContent.pause();
autoplayAllowed = true;
autoplayRequiresMuted = true;
autoplayChecksResolved();
}
function onMutedAutoplayFail() {
// Both muted and unmuted autoplay failed. Fall back to click to play.
videoContent.volume = 1;
videoContent.muted = false;
autoplayAllowed = false;
autoplayRequiresMuted = false;
autoplayChecksResolved();
}
function autoplayChecksResolved() {
// Request video ads.
var adsRequest = new google.ima.AdsRequest();
adsRequest.adTagUrl = <YOUR_AD_TAG_URL>;
...
adsRequest.setAdWillAutoPlay(autoplayAllowed);
adsRequest.setAdWillPlayMuted(autoplayRequiresMuted);
adsLoader.requestAds(adsRequest);
}
function onAdsManagerLoaded() {
...
if (autoplayAllowed) {
playAds();
} else {
playButton.style.display = 'block';
}
}
function playAds() {
try {
if (!adsInitialized) {
adDisplayContainer.initialize();
adsInitialized = true;
}
adsManager.init(640, 360);
adsManager.start();
} catch (adError) {
videoContent.play();
}
}
在 iPhone 上自动播放
除了之前的代码外,若要在 iPhone 上自动播放,你还需要
将 playsinline
参数添加到您的视频代码中。
index.html
<body>
...
<video id="contentElement" playsinline muted>
<source src="https://storage.googleapis.com/gvabox/media/samples/stock.mp4">
</video>
</body>
对 HTML 所做的更改可确保您的内容在内嵌视频中播放
Android 版播放器,而不是 iPhone 的默认全屏播放器。
自动播放广告和音频广告
请务必考虑在以下情况下,广告请求是否会返回纯音频广告:
您的广告可能会静音自动播放如果有机会
我们建议您采用以下任一方法:
- 将以下 VAST 网址参数
ad_type=video
更新为仅请求视频广告
广告(如果您的播放器支持视频)。如需详细了解网址参数
请参阅这份 Ad Manager 指南。
- 移除静音开始播放的广告选项。
请参阅 IMA 音频广告指南
敬请期待
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-10。
[null,null,["最后更新时间 (UTC):2025-08-10。"],[[["\u003cp\u003eAutoplay is supported on desktop and mobile web, but recent browser updates require websites to adjust their implementation.\u003c/p\u003e\n"],["\u003cp\u003eTo determine autoplay support, attempt to play the video and handle success or failure using Promises.\u003c/p\u003e\n"],["\u003cp\u003eIMA SDK provides settings to control ad autoplay behavior, allowing you to specify if ads should autoplay and whether they should be muted.\u003c/p\u003e\n"],["\u003cp\u003eOn iPhones, include the \u003ccode\u003eplaysinline\u003c/code\u003e attribute in the video tag to enable inline autoplay and avoid the default fullscreen player.\u003c/p\u003e\n"],["\u003cp\u003eConsider potential audio-only ads when autoplaying muted and adjust your ad request or settings accordingly to ensure a suitable user experience.\u003c/p\u003e\n"]]],[],null,["# Manage Autoplay\n\nAutoplay is supported on the desktop and on mobile web devices.\n\nAs of Chrome 53 and iOS 10, Android and iPhone support inline muted autoplay.\n\nSafari 11 for Desktop [has changed how it handles autoplay\nvideos](//webkit.org/blog/7734/auto-play-policy-changes-for-macos/). Google\nChrome [made a similar change](/web/updates/2017/09/autoplay-policy-changes)\nin April of 2018.\n\nIf your website currently autoplays video, update\nit to handle these new policies. The new [Attempt to Autoplay code\nsample](//github.com/googleads/googleads-ima-html5/tree/master/attempt_to_autoplay)\ndemonstrates how to attempt to autoplay a video and fall back to\nclick-to-play if autoplay fails. This guide walks you through the new sample.\n\nDetect autoplay success or failure in a browser\n-----------------------------------------------\n\nCurrently, web browsers do not offer a simple query to check if autoplay is\nsupported or not. The only way to check if a video can be autoplayed is to\ntry to play it.\n\nThis approach is demonstrated in the following code snippet: \n\n var contentVideo = document.getElementById('myVideo');\n var promise = contentVideo.play();\n\n if (promise !== undefined) {\n promise.then(_ =\u003e {\n // Autoplay worked!\n }).catch(error =\u003e {\n // Autoplay failed.\n });\n }\n\nThe code first calls `play()` on an HTML5 video element which returns a\n[`Promise`](/web/fundamentals/primers/promises). In our sample, the\n`Promise` is used to detect autoplay capability and set the\n[`AdsRequest`](/interactive-media-ads/docs/sdks/html5/client-side/reference/class/google.ima.AdsRequest)\nappropriately.\n\nAutoplay and IMA\n----------------\n\nThe IMA SDK `AdsRequest` has two fields pertinent to autoplay that you need to\nsupply:\n[`setAdWillAutoPlay`](/interactive-media-ads/docs/sdks/html5/client-side/reference/class/google.ima.AdsRequest#google.ima.AdsRequest.setAdWillAutoPlay)\nand [`setAdWillPlayMuted`](/interactive-media-ads/docs/sdks/html5/client-side/reference/class/google.ima.AdsRequest#google.ima.AdsRequest.setAdWillPlayMuted).\nThe former ensures that the ad server only returns ads that are allowed to\nbe autoplayed (if set to true), and the latter ensures that the ad server\nonly returns ads that are allowed to be started in a muted or unmuted state.\nOur sample uses the content video as an indicator of whether or not the browser\nsupports autoplay. Make two checks that lead to three potential outcomes:\n\nTo make these checks, try to play the content video and look at the returned\n`Promise`: \n\n var adsInitialized, autoplayAllowed, autoplayRequiresMuted;\n\n // Called on page load.\n function init() {\n videoContent = document.getElementById('contentElement');\n playButton = document.getElementById('playButton');\n // Hide the play button unless we need click-to-play.\n playButton.style.display = 'none';\n // Add an event listener now in case we need to fall back to click-to-play.\n playButton.addEventListener('click', () =\u003e {\n adDisplayContainer.initialize();\n adsInitialized = true;\n videoContent.load();\n playAds();\n });\n // Create your AdsLoader and AdDisplayContainer here.\n setUpIMA();\n // Check if autoplay is supported.\n checkAutoplaySupport();\n }\n\n function checkAutoplaySupport() {\n var playPromise = videoContent.play();\n if (playPromise !== undefined) {\n playPromise.then(onAutoplayWithSoundSuccess).catch(onAutoplayWithSoundFail);\n }\n }\n\n function onAutoplayWithSoundSuccess() {\n // If we make it here, unmuted autoplay works.\n videoContent.pause();\n autoplayAllowed = true;\n autoplayRequiresMuted = false;\n autoplayChecksResolved();\n }\n\n function onAutoplayWithSoundFail() {\n // Unmuted autoplay failed. Now try muted autoplay.\n checkMutedAutoplaySupport();\n }\n\n function checkMutedAutoplaySupport() {\n videoContent.volume = 0;\n videoContent.muted = true;\n var playPromise = videoContent.play();\n if (playPromise !== undefined) {\n playPromise.then(onMutedAutoplaySuccess).catch(onMutedAutoplayFail);\n }\n }\n\n function onMutedAutoplaySuccess() {\n // If we make it here, muted autoplay works but unmuted autoplay does not.\n videoContent.pause();\n autoplayAllowed = true;\n autoplayRequiresMuted = true;\n autoplayChecksResolved();\n }\n\n function onMutedAutoplayFail() {\n // Both muted and unmuted autoplay failed. Fall back to click to play.\n videoContent.volume = 1;\n videoContent.muted = false;\n autoplayAllowed = false;\n autoplayRequiresMuted = false;\n autoplayChecksResolved();\n }\n\n function autoplayChecksResolved() {\n // Request video ads.\n var adsRequest = new google.ima.AdsRequest();\n adsRequest.adTagUrl = \u003cYOUR_AD_TAG_URL\u003e;\n\n ...\n\n adsRequest.setAdWillAutoPlay(autoplayAllowed);\n adsRequest.setAdWillPlayMuted(autoplayRequiresMuted);\n adsLoader.requestAds(adsRequest);\n }\n\n function onAdsManagerLoaded() {\n ...\n if (autoplayAllowed) {\n playAds();\n } else {\n playButton.style.display = 'block';\n }\n }\n\n function playAds() {\n try {\n if (!adsInitialized) {\n adDisplayContainer.initialize();\n adsInitialized = true;\n }\n adsManager.init(640, 360);\n adsManager.start();\n } catch (adError) {\n videoContent.play();\n }\n }\n\nAutoplay on iPhone\n------------------\n\nIn addition to the previous code, autoplay on iPhone requires you\nto add the `playsinline` parameter to your video tag.\n\n#### index.html\n\n \u003cbody\u003e\n ...\n \u003cvideo id=\"contentElement\" playsinline muted\u003e\n \u003csource src=\"https://storage.googleapis.com/gvabox/media/samples/stock.mp4\"\u003e\n \u003c/video\u003e\n \u003c/body\u003e\n\nThis change to the HTML ensures that your content plays in an inline video\nplayer on iPhone, instead of iPhone's default fullscreen player.\n\nAutoplay and audio ads\n----------------------\n\nIt is important to consider whether an ad request will return audio only ads if\nthere is the possibility your ads will autoplay muted. If there is this chance,\nwe recommend one of the following:\n\n- Update the following VAST URL parameter `ad_type=video` to request only video ads (if your player supports video). For more information on URL parameters see this [Ad Manager guide](//support.google.com/admanager/answer/10678356).\n- Remove the option for ads to start muted.\n\nSee the [IMA audio ads guide](/interactive-media-ads/docs/sdks/html5/client-side/audio)\nfor more information on IMA audio integrations."]]