เล่นเสียงในเบื้องหลังในโฆษณาเสียง
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คำแนะนำนี้มีไว้สำหรับผู้เผยแพร่โฆษณา iOS ที่สนใจเพิ่มการเล่นโฆษณาเสียงในพื้นหลังลงในการติดตั้ง IMA SDK วิธีนี้ช่วยให้แอปขอโฆษณาในเบื้องหลังได้ ทั้งยังช่วยให้แอปสามารถเข้าสู่พื้นหลังและเล่นโฆษณาต่อไปจนจบ
เราไม่แนะนำให้เล่นโฆษณาวิดีโอในเบื้องหลัง
ข้อกำหนดเบื้องต้น
- แอปพลิเคชัน iOS ที่ใช้ IMA SDK
- IMA SDK V3 Beta v13 ขึ้นไป
เกริ่นนำที่เป็นประโยชน์
หากคุณยังต้องใช้ IMA SDK ในแอป โปรดอ่านคู่มือเริ่มต้นใช้งานของเรา
การเพิ่มการเล่นโฆษณาขณะล็อกหน้าจอหรือขณะใช้แอปอื่นลงในแอป
การเพิ่มการเล่นโฆษณาในเบื้องหลังมีขั้นตอนดังนี้
- เปิดใช้โหมดพื้นหลัง Audio และ Airplay ใน Xcode 6 ให้เลือกเป้าหมาย จากนั้นในส่วน ความสามารถ > โหมดพื้นหลัง ให้เปิดใช้ "เสียงและ Airplay"
- เปิดใช้งาน
AVAudioSession
และตั้งค่าหมวดหมู่ด้วย AVAudioSessionCategory
ที่เล่นเสียงในเบื้องหลังได้ เช่น 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);
}
}
- สร้าง
IMAAdsLoader
โดยการส่งในออบเจ็กต์ IMASettings
ที่มีการตั้งค่า enableBackgroundPlayback
เป็น YES
IMASettings *settings = [[IMASettings alloc] init];
settings.enableBackgroundPlayback = YES;
IMAAdsLoader *adsLoader = [[IMAAdsLoader alloc] initWithSettings:settings];
สำคัญ
คุณต้องเล่นเนื้อหาของคุณเพื่อสร้างคำขอโฆษณาในเบื้องหลัง โดยอาจต้องเรียก "เล่น" บนโปรแกรมเล่นเนื้อหาด้วยตนเองเมื่อแอปเข้าสู่พื้นหลังก่อนที่จะเรียกใช้ [IMAAdsLoader requestAds:]
การเล่นขณะล็อกหน้าจอหรือขณะใช้แอปอื่นในแอป iOS จะหยุดการเล่นโฆษณาชั่วคราวโดยอัตโนมัติ หากแอปทำงานอยู่เบื้องหลังขณะเล่นโฆษณา คุณต้องโทรหา [IMAAdsManager resume]
เพื่อเล่นต่อ
คำถามที่พบบ่อย
- จะเกิดอะไรขึ้นหากฉันไม่ได้เปิดใช้ความสามารถของโหมดการเล่นขณะล็อกหน้าจอหรือขณะใช้แอปอื่น (Audio และ Airplay) ในแอป
- หากคุณไม่เลือกโหมดนี้ เสียงที่แอปเล่นจะหยุดลงเมื่อแอปย้ายไปยังพื้นหลัง แอปจะไม่สามารถเปิดเสียงใดๆ ขณะที่อยู่ในเบื้องหลังด้วย
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 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."]]