Phát trong nền trong quảng cáo dạng âm thanh
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.
Hướng dẫn này dành cho các nhà xuất bản iOS muốn thêm tính năng phát quảng cáo âm thanh trong nền vào quá trình triển khai SDK IMA của họ. Điều này cho phép ứng dụng yêu cầu quảng cáo trong nền. Việc này cũng cho phép ứng dụng chuyển sang chạy ở chế độ nền và tiếp tục phát quảng cáo đến hết.
Bạn không nên phát quảng cáo dạng video trong nền.
Điều kiện tiên quyết
- Ứng dụng iOS có triển khai SDK IMA.
- SDK IMA V3 Beta phiên bản 13 trở lên.
Các đoạn mồi hữu ích
Nếu bạn vẫn cần triển khai SDK IMA trong ứng dụng của mình, hãy xem Hướng dẫn bắt đầu sử dụng của chúng tôi.
Thêm tính năng phát quảng cáo trong nền vào ứng dụng của bạn
Việc thêm tính năng phát quảng cáo trong nền cần thực hiện các bước sau:
- Bật chế độ nền Âm thanh và Airplay. Trong Xcode 6, hãy chọn một mục tiêu, sau đó trong phần Khả năng > Chế độ nền, bật "Âm thanh và Phát âm thanh".
- Kích hoạt
AVAudioSession
và đặt danh mục của lớp đó bằng AVAudioSessionCategory
có thể phát âm thanh nền, chẳng hạn như AVAudioSessionCategoryPlayback
.
- (void)viewDidLoad {
[super viewDidLoad];
NSError *error;
[[AVAudioSession sharedInstance] setActive:YES error:&error];
if (error != nil) {
NSLog(@"Error: %@", error.localizedDescription);
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
if (error != nil) {
NSLog(@"Error: %@", error.localizedDescription);
}
}
- Tạo
IMAAdsLoader
, truyền vào đối tượng IMASettings
có enableBackgroundPlayback
được đặt thành YES
.
IMASettings *settings = [[IMASettings alloc] init];
settings.enableBackgroundPlayback = YES;
IMAAdsLoader *adsLoader = [[IMAAdsLoader alloc] initWithSettings:settings];
Quan trọng
Để thực hiện yêu cầu quảng cáo trong nền, nội dung của bạn phải đang phát. Có thể bạn sẽ phải gọi thủ công tính năng phát trên trình phát nội dung khi ứng dụng chuyển sang chạy trong nền trước khi gọi [IMAAdsLoader requestAds:]
.
Khi ứng dụng iOS chạy ở chế độ nền, hệ thống sẽ tự động tạm dừng việc phát quảng cáo. Nếu ứng dụng của bạn chạy ở chế độ nền trong khi phát quảng cáo, thì bạn cần gọi [IMAAdsManager resume]
để tiếp tục phát.
Câu hỏi thường gặp
- Điều gì xảy ra nếu tôi không bật tính năng chế độ nền Âm thanh và Airplay trong ứng dụng của mình?
- Nếu bạn không chọn chế độ này, mọi âm thanh mà ứng dụng phát sẽ dừng khi ứng dụng chuyển sang chạy trong nền. Ứng dụng cũng không chạy được âm thanh khi ở chế độ nền.
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\u003eThis guide helps iOS publishers add background audio ad playback to their IMA SDK implementation.\u003c/p\u003e\n"],["\u003cp\u003eRequires iOS application with IMA SDK V3 Beta v13 or greater.\u003c/p\u003e\n"],["\u003cp\u003eEnable "Audio and Airplay" background mode in Xcode and activate \u003ccode\u003eAVAudioSession\u003c/code\u003e for background audio playback.\u003c/p\u003e\n"],["\u003cp\u003eCreate an \u003ccode\u003eIMAAdsLoader\u003c/code\u003e with \u003ccode\u003eenableBackgroundPlayback\u003c/code\u003e set to \u003ccode\u003eYES\u003c/code\u003e in \u003ccode\u003eIMASettings\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eBackgrounding pauses ad playback; resume with \u003ccode\u003e[IMAAdsManager resume]\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Play background in audio ads\n\nThis guide is intended for iOS publishers interested in adding background **audio** ad playback to their IMA SDK implementation. This allows the app to request ads in the background. It also allow the app to enter the background and continue to play an ad to completion.\n\nWe do not recommend playing video ads in the background.\n\nPrerequisites\n-------------\n\n\u003cbr /\u003e\n\n- iOS application with the IMA SDK implemented.\n- IMA SDK V3 Beta v13 or greater.\n\n\u003cbr /\u003e\n\nHelpful primers\n---------------\n\nIf you still need to implement the IMA SDK in your app, check out our [Get Started guide](/interactive-media-ads/docs/sdks/ios/client-side).\n\nAdding background ad playback to your app\n-----------------------------------------\n\nAdding background ad playback takes the following steps:\n\n1. Enable the **Audio and Airplay** background mode. In Xcode 6, select a target, then under **Capabilities \\\u003e Background Modes**, enable \"Audio and Airplay\".\n2. Activate the `AVAudioSession`, and set its category with an `AVAudioSessionCategory` that can play background audio, such as `AVAudioSessionCategoryPlayback`. \n\n```objective-c\n- (void)viewDidLoad {\n [super viewDidLoad];\n\n NSError *error;\n [[AVAudioSession sharedInstance] setActive:YES error:&error];\n if (error != nil) {\n NSLog(@\"Error: %@\", error.localizedDescription);\n }\n\n [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];\n if (error != nil) {\n NSLog(@\"Error: %@\", error.localizedDescription);\n }\n}\n```\n3. Create an `IMAAdsLoader`, passing in an `IMASettings` object with `enableBackgroundPlayback` set to `YES`. \n\n```objective-c\n IMASettings *settings = [[IMASettings alloc] init];\n settings.enableBackgroundPlayback = YES;\n IMAAdsLoader *adsLoader = [[IMAAdsLoader alloc] initWithSettings:settings];\n```\n\nImportant\n---------\n\nTo make background ad requests, your content must be playing. This may require manually calling play on the content player when the app enters the background before calling `[IMAAdsLoader requestAds:]`.\n\nBackgrounding an iOS app automatically pauses ad playback. If your app is backgrounded while playing an ad, you need to call `[IMAAdsManager resume]` to resume playback.\n\nFAQ\n---\n\nWhat happens if I don't enable the Audio and Airplay background mode capability in my app?\n: If you don't select this mode, any audio being played by the app stops when the app moves to the background. The app is also not able to launch any audio while backgrounded."]]