Lire en arrière-plan dans les annonces audio
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Ce guide est destiné aux éditeurs iOS qui souhaitent ajouter la lecture d'annonces audio en arrière-plan à leur implémentation du SDK IMA. Cela permet à l'application de demander des annonces en arrière-plan. De plus, l'application peut passer en arrière-plan et poursuivre la lecture de l'annonce jusqu'à la fin.
Nous vous déconseillons de lire des annonces vidéo en arrière-plan.
Prérequis
- Application iOS intégrant le SDK IMA
- SDK IMA version 3 bêta 13 ou ultérieure.
Introductions utiles
Si vous avez encore besoin d'implémenter le SDK IMA dans votre application, consultez notre guide de démarrage.
Activer la lecture des annonces en arrière-plan dans votre application
Pour activer la lecture des annonces en arrière-plan, procédez comme suit:
- Activez le mode d'arrière-plan Audio et AirPlay. Dans Xcode 6, sélectionnez une cible, puis sous Fonctionnalités > Modes d'arrière-plan, activez "Audio et AirPlay".
- Activez le
AVAudioSession
et définissez sa catégorie avec un AVAudioSessionCategory
capable de lire des contenus audio en arrière-plan, comme 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);
}
}
- Créez un
IMAAdsLoader
en transmettant un objet IMASettings
avec enableBackgroundPlayback
défini sur YES
.
IMASettings *settings = [[IMASettings alloc] init];
settings.enableBackgroundPlayback = YES;
IMAAdsLoader *adsLoader = [[IMAAdsLoader alloc] initWithSettings:settings];
Important
Pour envoyer des demandes d'annonces en arrière-plan, votre contenu doit être en cours de lecture. Vous devrez peut-être appeler manuellement la lecture sur le lecteur de contenu lorsque l'application passe en arrière-plan avant d'appeler [IMAAdsLoader requestAds:]
.
La mise en arrière-plan d'une application iOS met automatiquement en pause la lecture des annonces. Si votre application est exécutée en arrière-plan pendant la lecture d'une annonce, vous devez appeler [IMAAdsManager resume]
pour reprendre la lecture.
Questions fréquentes
- Que se passe-t-il si je n'active pas les modes audio et AirPlay en arrière-plan dans mon application ?
- Si vous ne sélectionnez pas ce mode, le contenu audio lu par l'application s'arrête lorsque l'application passe en arrière-plan. L'application ne peut pas non plus lancer de contenu audio en arrière-plan.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/08/31 (UTC).
[null,null,["Dernière mise à jour le 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."]]