自動化廣告播放清單
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
IMA HTML5 SDK 支援全自動廣告播放清單。這項功能會在您放送廣告時,將 Google Ad Manager 中指定的廣告插播插入內容中。這也大幅簡化了支援廣告插播所需的影片播放器程式碼,包括片頭、片中和片尾廣告。
- 建立
AdsManager
時,系統會使用 getAdsManager 呼叫傳入 contentPlayback
物件。這個物件必須具有 currentTime
屬性,可傳回影片目前的播放頭位置。如果您使用 HTML5 video
元素顯示內容,可以直接將該元素傳遞至 SDK。這個物件可用於追蹤內容播放進度,以便在 Ad Manager 指定的時間自動插入廣告插播。您也必須讓 SDK 知道您希望由 SDK 代為處理內容狀態。var adsRenderingSettings = new google.ima.AdsRenderingSettings();
adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;
adsManager = adsManagerLoadedEvent.getAdsManager(
videoContent, adsRenderingSettings); // See API reference for contentPlayback.
- 為確保片尾廣告播放,您必須讓 SDK 知道內容播放完畢的時間。這有點棘手,因為在某些情況下,SDK 會使用您的影片播放器播放廣告,因此您必須確保只讓 SDK 知道內容播放完畢的時間,而非廣告播放完畢的時間。您可以使用下列程式碼執行這項操作:
var videoContent = document.getElementById('contentElement');
var contentEndedListener = function() {adsLoader.contentComplete();};
videoContent.addEventListener('ended', contentEndedListener);
function onContentPauseRequested() {
contentElement.removeEventListener('ended', contentEndedListener);
...
}
function onContentResumeRequested() {
contentElement.addEventListener('ended', contentEndedListener);
...
}
- 播放廣告插播時,系統會使用
CONTENT_PAUSE_REQUESTED
和 CONTENT_RESUME_REQUESTED
事件暫停及繼續播放內容。
- 如果影片播放器支援拖曳尋找功能,且影片播放器的目前時間屬性在使用者拖曳時更新,SDK 就無法區分內容是否正在正常播放,以及使用者是否正在尋找內容。您必須使用自訂 contentPlayback 物件做為
getAdsManager
的參數。如需此用法的範例,請參閱「尋找問題」。
注意:當內容播放完畢或使用者停止播放時,請務必呼叫 AdsLoader.contentComplete,以便向 SDK 發出內容已完成的信號。接著,如果已排定片尾廣告插播時間,SDK 就會播放片尾廣告插播。播放完所有廣告插播後,系統會觸發 ALL_ADS_COMPLETED
事件。此外,請注意,內容追蹤會在呼叫 init()
時開始,因此請務必在播放內容前呼叫 init()
。
停用自動播放廣告插播功能
在某些情況下,您可能會希望 SDK 在您準備好播放廣告插播前,不要播放廣告插播。在這種情況下,您可以停用自動播放廣告插播功能,並讓 SDK 知道何時播放廣告插播。在這個設定中,SDK 載入廣告插播後,就會觸發 AD_BREAK_READY
事件。當播放器準備好開始廣告插播時,您可以呼叫 adsManager.start():
function requestAds() {}
...
adsLoader.getSettings().setAutoPlayAdBreaks(false);
...
}
function onAdsManagerLoaded() {
...
// For non-auto ad breaks, listen for ad break ready
adsManager.addEventListener(
google.ima.AdEvent.Type.AD_BREAK_READY,
adBreakReadyHandler);
...
}
function adBreakReadyHandler() {
// Once we're ready to play ads. To skip this ad break, simply return
// from this handler without calling adsManager.start().
adsManager.start();
}
立即試用
請參閱下列程式碼,瞭解如何實作。
尋找問題
如果你使用廣告規則,可能會遇到點選拖曳尋找的問題。具體來說,當使用者點選並拖曳以在影片中跳過多個中插廣告時,可能會看到 2 個以上的中插廣告連續播放,然後才會繼續播放內容。這是因為影片播放頭時間會在使用者暫停時更新;如果 SDK 在使用者暫停廣告後,恰好輪詢目前時間,可能會認為應播放廣告。內容繼續播放時,系統會播放該廣告,然後播放從快轉點開始播放的最新廣告。如需這項問題的視覺化呈現方式,請參閱下列圖表:
請在使用者開始尋找時儲存目前的時間,並在 SDK 要求時回報該時間,直到使用者恢復正常播放為止。如需這項解決方案的視覺化呈現方式,請參閱下圖:
透過這項解決方案,您可以正確略過 0:10 中插廣告,並只播放 0:20 中插廣告。如要完成這項操作,請在下列程式碼片段中使用自訂播放頭追蹤器。這個程式碼包含進階 HTML5 範例中的 ads.js
修改內容 (以粗體顯示),該範例可在下載頁面中找到。
var Ads = function(application, videoPlayer) {
...
this.currentTime = 0;
setInterval(this.updateCurrentTime, 1000);
};
Ads.prototype.updateCurrentTime = function() {
if (!this.videoPlayer_.contentPlayer.seeking) {
this.currentTime = this.videoPlayer_.contentPlayer.currentTime;
}
};
Ads.prototype.onAdsManagerLoaded_ = function(adsManagerLoadedEvent) {
this.application_.log('Ads loaded.');
this.adsManager_ = adsManagerLoadedEvent.getAdsManager(this);
this.processAdsManager_(this.adsManager_);
};
Safari 行動版的已知問題
這個方法應該適用於所有平台,除了行動版 Safari 以外。在行動版 Safari 上,影片標記的尋找屬性未正確導入 (一律傳回 false)。為避免這種情況,您必須自行檢查,確認使用者是否正在尋找影片。以下是這個方法的程式碼範例。再次提醒,加粗的程式碼行是對現有程式碼的修改。
var Ads = function(application, videoPlayer) {
...
this.currentTime = 0;
setInterval(this.updateCurrentTime, 1000);
this.seeking = false;
this.seekCheckInterval = 1000;
// You may need to adjust this value, depending on your platform
this.seekThreshold = 100;
this.previousTime = 0;
setInterval(
Application.bind(this, this.checkForSeeking),
this.seekCheckInterval);
};
Ads.prototype.updateCurrentTime = function() {
if (!this.seeking) {
this.currentTime = this.videoPlayer_.contentPlayer.currentTime;
}
};
Ads.prototype.checkForSeeking = function() {
var currentTime = this.videoPlayer_.contentPlayer.currentTime;
// How much time has passed since you last ran this method, in milliseconds
var diff = (currentTime - this.previousTime) * 1000;
// If that difference is greater than the time since you last ran this method,
// plus the threshold, the user was seeking
if (Math.abs(diff) > this.interval + this.threshold) {
this.seeking = true;
} else {
this.seeking = false;
}
// Grab the current video time again to make up for time spent in this method
previousTime = this.videoPlayer_.contentPlayer.currentTime;
};
Ads.prototype.onAdsManagerLoaded_ = function(adsManagerLoadedEvent) {
this.application_.log('Ads loaded.');
this.adsManager_ = adsManagerLoadedEvent.getAdsManager(this);
this.processAdsManager_(this.adsManager_);
};
在進行這些變更後,SDK 會使用 Ads
物件的 currentTime 屬性,判斷何時播放廣告插播,而非內容影片播放器的 currentTime
屬性。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eThe IMA HTML5 SDK enables automated ad playlists, simplifying ad integration and supporting pre-rolls, mid-rolls, and post-rolls by leveraging Google Ad Manager.\u003c/p\u003e\n"],["\u003cp\u003eTo ensure proper ad playback, developers must signal content completion using \u003ccode\u003eAdsLoader.contentComplete()\u003c/code\u003e and manage content state synchronization with the SDK.\u003c/p\u003e\n"],["\u003cp\u003eCustom content playback objects can be used to address seeking issues and provide finer control over ad break timing and behavior.\u003c/p\u003e\n"],["\u003cp\u003eThe SDK provides events like \u003ccode\u003eCONTENT_PAUSE_REQUESTED\u003c/code\u003e, \u003ccode\u003eCONTENT_RESUME_REQUESTED\u003c/code\u003e, and \u003ccode\u003eAD_BREAK_READY\u003c/code\u003e for developers to manage content playback and ad interactions.\u003c/p\u003e\n"],["\u003cp\u003eSpecific code adjustments are necessary to handle seeking accurately on mobile Safari due to platform limitations.\u003c/p\u003e\n"]]],[],null,["# Automate ad playlists\n\nIMA HTML5 SDK supports fully automated ad playlists. This feature\ninserts ad breaks into the content as specified in\n[Google Ad Manager](//www.google.com/dfp)\nwhen trafficking your ads. It also greatly simplifies the video player code\nnecessary to support ad breaks, including pre-rolls, mid-rolls and post-rolls.\n\n- When creating the `AdsManager`, a `contentPlayback` object is passed in using the [getAdsManager](/interactive-media-ads/docs/sdks/html5/client-side/reference/interface/google.ima.AdsManagerLoadedEvent#google.ima.AdsManagerLoadedEvent.getAdsManager) call. This object must have a `currentTime` property that returns the current playhead position of the video. If you're using an HTML5 `video` element to display your content, you can just pass that element to the SDK. This object is used to track the progress of the content playback so ad breaks are automatically inserted at the times specified in Ad Manager. You also need to let the SDK know that you want it to handle content state on your behalf. \n\n ```javascript\n var adsRenderingSettings = new google.ima.AdsRenderingSettings();\n adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;\n adsManager = adsManagerLoadedEvent.getAdsManager(\n videoContent, adsRenderingSettings); // See API reference for /interactive-media-ads/docs/sdks/html5/client-side/reference/interface/google.ima.AdsManagerLoadedEvent#google.ima.AdsManagerLoadedEvent.getAdsManager.\n ```\n- To ensure post-rolls are played, you need to let the SDK know when your content is finished. This is a bit tricky, because in some cases the SDK uses your video player to play ads, so you need to make sure you're only letting the SDK know when your content is finished, and not when an ad is finished. You can do that using the following code: \n\n ```javascript\n var videoContent = document.getElementById('contentElement');\n var contentEndedListener = function() {adsLoader.contentComplete();};\n\n videoContent.addEventListener('ended', contentEndedListener);\n\n function onContentPauseRequested() {\n contentElement.removeEventListener('ended', contentEndedListener);\n ...\n }\n\n function onContentResumeRequested() {\n contentElement.addEventListener('ended', contentEndedListener);\n ...\n }\n ```\n- The [CONTENT_PAUSE_REQUESTED](/interactive-media-ads/docs/sdks/html5/client-side/reference/namespace/google.ima.AdEvent#google.ima.AdEvent.Type) and [CONTENT_RESUME_REQUESTED](/interactive-media-ads/docs/sdks/html5/client-side/reference/namespace/google.ima.AdEvent#google.ima.AdEvent.Type) events are used to pause and resume the content when ad breaks are played.\n- If your video player supports drag-to-seek, and the current time property of the video player updates while the user is dragging, the SDK can't differentiate between content progressing normally and a user seeking through the content. You must use a custom contentPlayback object as your parameter to `getAdsManager`. For an example of this use case, see [The Trouble with Seeking](#trouble-with-seeking).\n\n**Note:** When the content has finished playing or\nthe user has stopped playback, be sure to call AdsLoader.contentComplete\nin order to signal to the SDK that the content is done. The SDK then plays\nthe post-roll ad break, if one has been scheduled. The `ALL_ADS_COMPLETED`\nevent is raised when ALL ad breaks have been played. In addition, note\nthat content tracking begins when `init()` is called and you should\nalways call `init()` before playing content.\n\nDisable automatic playback of ad breaks\n---------------------------------------\n\nIn some circumstances you may want to prevent the SDK from playing ad breaks until\nyou're ready for them. In this scenario, you can disable automatic playback of ad breaks\nin favor of letting the SDK know when you're ready for an ad break to play. With this\nconfiguration, once the SDK has loaded an ad break, it fires an\n`AD_BREAK_READY` event. When your player is ready for the ad break to start,\nyou can call adsManager.start(): \n\n```javascript\nfunction requestAds() {}\n ...\n adsLoader.getSettings().setAutoPlayAdBreaks(false);\n ...\n}\n\nfunction onAdsManagerLoaded() {\n ...\n // For non-auto ad breaks, listen for ad break ready\n adsManager.addEventListener(\n google.ima.AdEvent.Type.AD_BREAK_READY,\n adBreakReadyHandler);\n ...\n}\n\nfunction adBreakReadyHandler() {\n // Once we're ready to play ads. To skip this ad break, simply return\n // from this handler without calling adsManager.start().\n adsManager.start();\n}\n```\n\nTry it out\n----------\n\nSee the following code for a working implementation.\nSee the Pen \\\u003ca href='http://codepen.io/imasdk/pen/QyBNrq/'\\\u003eManual Ad Breaks\\\u003c/a\\\u003e by IMA SDK (\\\u003ca href='http://codepen.io/imasdk'\\\u003e@imasdk\\\u003c/a\\\u003e) on \\\u003ca href='http://codepen.io'\\\u003eCodePen\\\u003c/a\\\u003e.\n\nThe trouble with seeking\n------------------------\n\nIf you use ad rules, you may run into a problem with click-and-drag seeking.\nSpecifically, after a user clicks and drags to seek through video past multiple\nmidroll pods, they may see 2 or more of those pods play back to back before\ncontent resumes. This is caused by the video playhead time updating while the\nuser is seeking; if the SDK happens to poll for the current time while the user\nseeks past an ad, it may think that ad should be played. When the content\nresumes, it plays that ad, and then the most recent ad since the seek. For a\nvisual representation of this problem, see the following diagram:\n\nSave the current time when the user starts seeking, and report that time when the SDK\nasks for it until the user resumes normal playback. For a visual representation of this\nsolution, see the following diagram:\n\nWith this solution, you properly skip the 0:10 mid-roll and only play the 0:20 mid-roll.\nThis is done using a custom playhead tracker in the following code snippet. This code\ncontains modifications (shown in bold) of `ads.js` in the advanced HTML5\nsample available on our\n[download page](/interactive-media-ads/docs/sdks/html5/download). \n\n```javascript\nvar Ads = function(application, videoPlayer) {\n ...\n this.currentTime = 0;\n setInterval(this.updateCurrentTime, 1000);\n};\n\nAds.prototype.updateCurrentTime = function() {\n if (!this.videoPlayer_.contentPlayer.seeking) {\n this.currentTime = this.videoPlayer_.contentPlayer.currentTime;\n }\n};\n\nAds.prototype.onAdsManagerLoaded_ = function(adsManagerLoadedEvent) {\n this.application_.log('Ads loaded.');\n this.adsManager_ = adsManagerLoadedEvent.getAdsManager(this);\n this.processAdsManager_(this.adsManager_);\n};\n```\n\n### Known issues with mobile Safari\n\nThis method should work on every plaform except mobile Safari. On mobile\nSafari, the seeking property of the video tag is not properly implemented (it\nalways returns false). To get around that, you need to do your own check to\nsee if the user is seeking through the video. The sample code for this method\nfollows. Again, the bolded lines are modifications to existing code. \n\n```javascript\nvar Ads = function(application, videoPlayer) {\n ...\n this.currentTime = 0;\n setInterval(this.updateCurrentTime, 1000);\n this.seeking = false;\n this.seekCheckInterval = 1000;\n // You may need to adjust this value, depending on your platform\n this.seekThreshold = 100;\n this.previousTime = 0;\n setInterval(\n Application.bind(this, this.checkForSeeking),\n this.seekCheckInterval);\n};\n\nAds.prototype.updateCurrentTime = function() {\n if (!this.seeking) {\n this.currentTime = this.videoPlayer_.contentPlayer.currentTime;\n }\n};\n\nAds.prototype.checkForSeeking = function() {\n var currentTime = this.videoPlayer_.contentPlayer.currentTime;\n // How much time has passed since you last ran this method, in milliseconds\n var diff = (currentTime - this.previousTime) * 1000;\n // If that difference is greater than the time since you last ran this method,\n // plus the threshold, the user was seeking\n if (Math.abs(diff) \u003e this.interval + this.threshold) {\n this.seeking = true;\n } else {\n this.seeking = false;\n }\n // Grab the current video time again to make up for time spent in this method\n previousTime = this.videoPlayer_.contentPlayer.currentTime;\n};\n\nAds.prototype.onAdsManagerLoaded_ = function(adsManagerLoadedEvent) {\n this.application_.log('Ads loaded.');\n this.adsManager_ = adsManagerLoadedEvent.getAdsManager(this);\n this.processAdsManager_(this.adsManager_);\n};\n```\n\nWith these changes, the SDK is now using the currentTime property of your `Ads`\nobject to determine when to play ad breaks, not the `currentTime` property of the\ncontent video player."]]