管理自動播放功能
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
電腦版和行動版網站皆支援自動播放功能。
自 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.
});
}
程式碼會先呼叫 HTML5 影片元素上的 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>
這項變更可確保您的內容在內嵌影片中播放。
iPhone 播放器,而不是 iPhone 的預設全螢幕播放器。
自動播放和音訊廣告
請務必考慮符合以下條件的廣告請求:
您的廣告可能會自動靜音如果發生這種情況
建議您採用下列其中一種做法:
- 更新下列 VAST 網址參數
ad_type=video
,只請求影片
廣告 (如果您的播放器支援影片)。進一步瞭解網址參數
請參閱這篇 Ad Manager 指南。
- 移除廣告靜音選項。
請參閱 IMA 音訊廣告指南
。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-10 (世界標準時間)。
[null,null,["上次更新時間: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."]]