Tự động hoá danh sách phát quảng cáo
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
SDK Android IMA hỗ trợ danh sách phát quảng cáo hoàn toàn tự động. Tính năng này
chèn điểm chèn quảng cáo vào nội dung như đã chỉ định trong
Google Ad Manager
khi quản lý quảng cáo. Công cụ này cũng đơn giản hoá đáng kể mã trình phát video
cần thiết để hỗ trợ các điểm chèn quảng cáo, bao gồm cả quảng cáo trước video, trong video và sau video.
- Khi quản lý quảng cáo trong Ad Manager, bạn có thể chỉ định các quy tắc quảng cáo khác nhau
chẳng hạn như "luôn phát điểm chèn quảng cáo ở đầu nội dung" hoặc "phát một
phút chèn quảng cáo mỗi 30 phút nội dung".
- Khi quảng cáo được yêu cầu, máy chủ quảng cáo có thể trả về một danh sách phát quảng cáo. SDK
xử lý danh sách phát và tự động lên lịch cho các điểm chèn quảng cáo đã
đã chỉ định.
- Vì Android sử dụng cùng một trình phát video cho cả việc phát quảng cáo lẫn nội dung,
nếu định triển khai quy tắc quảng cáo, bạn phải lưu vị trí con trỏ vị trí của
nội dung khi quảng cáo bắt đầu, sau đó tìm đến vị trí đó khi quảng cáo kết thúc.
Hãy nhớ triển khai giao diện
VideoAdPlayer
trong video của bạn
trình phát. Điều này đảm bảo rằng điểm chèn quảng cáo sẽ tự động được chèn vào
thời gian quy định trong 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
và CONTENT_RESUME_REQUESTED
các sự kiện được dùng để tạm dừng và tiếp tục nội dung khi các điểm chèn quảng cáo được phát. Giới thiệu
theo tài liệu API có liên quan
để biết thông tin chi tiết về các sự kiện này.
Lưu ý: Khi nội dung đã phát xong, hoặc
người dùng đã dừng phát, hãy nhớ gọi
AdsLoader.contentComplete
để báo cho SDK biết rằng nội dung đã hoàn tất. SDK
sau đó phát điểm chèn quảng cáo sau video nếu đã được lên lịch. Chiến lược phát hành đĩa đơn
Sự kiện ALL_ADS_COMPLETED
được thực hiện khi TẤT CẢ điểm chèn quảng cáo
đã được chơi. Ngoài ra, xin lưu ý rằng hoạt động theo dõi nội dung bắt đầu khi
init()
sẽ được gọi và bạn phải luôn gọi init()
trước khi phát nội dung.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\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."]]