This 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.
We do not recommend playing video ads in the background.
Prerequisites
- iOS application with the IMA SDK implemented.
- IMA SDK V3 Beta v13 or greater.
Helpful primers
If you still need to implement the IMA SDK in your app, check out our Get Started guide.
Adding background ad playback to your app
Adding background ad playback takes the following steps:
- Enable the Audio and Airplay background mode. In Xcode 6, select a target, then under Capabilities > Background Modes, enable "Audio and Airplay".
- Activate the
AVAudioSession
, and set its category with anAVAudioSessionCategory
that can play background audio, such asAVAudioSessionCategoryPlayback
. - Create an
IMAAdsLoader
, passing in anIMASettings
object withenableBackgroundPlayback
set toYES
.
- (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); } }
IMASettings *settings = [[IMASettings alloc] init]; settings.enableBackgroundPlayback = YES; IMAAdsLoader *adsLoader = [[IMAAdsLoader alloc] initWithSettings:settings];
Important
To 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:]
.
Backgrounding 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.
FAQ
- What happens if I don't enable the Audio and Airplay background mode capability in my app?
- 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.