自动制作广告播放列表
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
IMA Android SDK 支持完全自动化的广告播放列表。此功能
按照
Google Ad Manager
。它还大大简化了视频播放器代码
(包括前贴片广告、中贴片广告和后贴片广告)。
- 在 Ad Manager 中投放广告时,可以指定各种广告规则
如“始终在内容开头处播放广告插播时间点”或“播放一个
每 30 分钟内容插播一分钟广告”。
- 请求广告时,广告服务器可返回广告播放列表。SDK
会处理播放列表,并自动安排
。
- 由于 Android 使用相同的视频播放器来播放广告和内容,
如果您打算实施广告规则,则必须保存
内容,然后在广告结束时跳转到该位置。
请务必在您的视频中实现
VideoAdPlayer
接口
。这样可确保在
Ad Manager 中指定的时间。
private boolean playingContent = true;
private int contentPosition = -1;
private List callbacks =
new ArrayList();
@Override
public void addCallback(VideoAdPlayerCallback callback) {
callbacks.add(callback);
}
@Override
public void removeCallback(VideoAdPlayerCallback callback) {
callbacks.remove(callback);
}
public void loadContent() {
playingContent = true;
load(CONTENT_URL);
}
@Override
public void loadAd(String mediaUrl) {
playingContent = false;
load(mediaUrl);
}
public void pauseContent() {
savePosition();
pause();
}
@Override
public void pauseAd() {
pause();
}
private void pause() {
myVideoView.pause();
for (VideoAdPlayerCallback callback : callbacks) {
callback.onPause();
}
}
public void resumeContent() {
loadContent();
if (contentPosition > 0) {
restorePosition();
}
resume();
}
@Override
public void resumeAd() {
resume();
}
private void resume() {
myVideoView.start();
for (VideoAdPlayerCallback callback : callbacks) {
callback.onResume();
}
}
public void savePosition() {
contentPosition = myVideoView.getCurrentPosition();
}
public void restorePosition() {
myVideoView.seekTo(contentPosition);
}
CONTENT_PAUSE_REQUESTED
和CONTENT_RESUME_REQUESTED
事件用于在广告插播时间点播放时暂停和恢复内容。推荐
参阅相关的 API 文档
了解这些事件的详情。
注意:当内容播放完毕或
则在用户已停止播放时,请务必调用
AdsLoader.contentComplete
,以便向 SDK 表明内容已完成。SDK
然后播放后贴片广告插播时间点(如果已安排)。通过
当所有广告插播时间点触发时引发 ALL_ADS_COMPLETED
事件
已播放过的内容。另外请注意,内容跟踪会在
系统会调用 init()
,并且您应始终调用 init()
然后再播放内容
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eIMA Android SDK simplifies video ad integration by automating ad playlist insertion and playback, including pre-rolls, mid-rolls, and post-rolls.\u003c/p\u003e\n"],["\u003cp\u003eAd breaks are scheduled based on rules defined in Google Ad Manager, enabling features like pre-roll ads and periodic mid-rolls.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers must implement the \u003ccode\u003eVideoAdPlayer\u003c/code\u003e interface and manage content position during ad breaks for seamless transitions between ads and content.\u003c/p\u003e\n"],["\u003cp\u003eThe SDK provides events (\u003ccode\u003eCONTENT_PAUSE_REQUESTED\u003c/code\u003e, \u003ccode\u003eCONTENT_RESUME_REQUESTED\u003c/code\u003e) to handle content playback during ad breaks and signals ad completion with \u003ccode\u003eALL_ADS_COMPLETED\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eContent tracking begins with \u003ccode\u003einit()\u003c/code\u003e, which should be called before content playback, and \u003ccode\u003econtentComplete()\u003c/code\u003e should be called after content ends to trigger post-rolls.\u003c/p\u003e\n"]]],[],null,["# Automate ad playlists\n\nIMA Android SDK supports fully automated ad playlists. This feature\ninserts ad breaks into the content as specified in\n[Google Ad Manager](//admanager.google.com/)\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 trafficking the ads in Ad Manager, it is possible to specify various ad rules like \"always play ad break at the beginning of the content\" or \"play a one minute ad break every 30 minutes of content\".\n- When ads are requested, the ad server can return an ad playlist. The SDK processes the playlist and automatically schedules the ad breaks that have been specified.\n- Since Android uses the same video player for both ad and content playback, if you plan to implement ad rules, you must save the playhead position of your content when an ad starts, then seek to that position when the ad finishes. Be sure to implement the `VideoAdPlayer` interface in your video player. This ensures that ad breaks are automatically inserted at the times specified in Ad Manager. \n\n ```gdscript\n private boolean playingContent = true;\n private int contentPosition = -1;\n private List callbacks =\n new ArrayList();\n\n @Override\n public void addCallback(VideoAdPlayerCallback callback) {\n callbacks.add(callback);\n }\n\n @Override\n public void removeCallback(VideoAdPlayerCallback callback) {\n callbacks.remove(callback);\n }\n\n public void loadContent() {\n playingContent = true;\n load(CONTENT_URL);\n }\n\n @Override\n public void loadAd(String mediaUrl) {\n playingContent = false;\n load(mediaUrl);\n }\n\n public void pauseContent() {\n savePosition();\n pause();\n }\n\n @Override\n public void pauseAd() {\n pause();\n }\n\n private void pause() {\n myVideoView.pause();\n for (VideoAdPlayerCallback callback : callbacks) {\n callback.onPause();\n }\n }\n\n public void resumeContent() {\n loadContent();\n if (contentPosition \u003e 0) {\n restorePosition();\n }\n resume();\n }\n\n @Override\n public void resumeAd() {\n resume();\n }\n\n private void resume() {\n myVideoView.start();\n for (VideoAdPlayerCallback callback : callbacks) {\n callback.onResume();\n }\n }\n\n public void savePosition() {\n contentPosition = myVideoView.getCurrentPosition();\n }\n\n public void restorePosition() {\n myVideoView.seekTo(contentPosition);\n }\n ```\n- The `CONTENT_PAUSE_REQUESTED` and `CONTENT_RESUME_REQUESTED` events are used to pause and resume the content when ad breaks are played. Refer to the relevant [API documentation](/interactive-media-ads/docs/sdks/android/client-side/api/reference/com/google/ads/interactivemedia/v3/api/AdEvent.AdEventType) for details on these events.\n\n**Note:** When the content has finished playing or\nthe user has stopped playback, be sure to call\n[AdsLoader.contentComplete](/interactive-media-ads/docs/sdks/android/client-side/api/reference/com/google/ads/interactivemedia/v3/api/AdsLoader#contentComplete())\nin order to signal to the SDK that the content is done. The SDK\nthen plays the post-roll ad break, if one has been scheduled. The\n`ALL_ADS_COMPLETED` event is raised when ALL ad breaks\nhave been played. In addition, note that content tracking begins when\n`init()` is called and you should always call `init()`\nbefore playing content."]]