Conditions préalables
- Intégrez le format d'annonce native.
GADMediaContent
Les annonces natives donnent accès à une classe GADMediaContent
qui permet d'obtenir des informations sur du contenu multimédia, par exemple une vidéo ou une image. Il permet également de contrôler la lecture des annonces vidéo et l'écoute des événements de lecture. Vous pouvez accéder à l'objet de contenu multimédia via la propriété .mediaContent
de l'annonce.
L'objet GADMediaContent
contient des informations telles que le format et la durée de la vidéo. L'extrait de code ci-dessous montre comment obtenir les proportions et la durée d'une annonce native.
Swift
if myNativeAd.mediaContent.hasVideoContent { let mediaAspectRatio = CGFloat(myNativeAd.mediaContent.aspectRatio) let duration = myNativeAd.mediaContent.duration ... }
Objective-C
if(myNativeAd.mediaContent.hasVideoContent) { CGFloat mediaAspectRatio = myNativeAd.mediaContent.aspectRatio; NSTimeInterval duration = myNativeAd.mediaContent.duration; ... }
GADVideoController
L'objet GADMediaContent
fait référence à un objet GADVideoController
. L'objet GADVideoController
permet aux éditeurs de répondre aux événements vidéo.
Vous pouvez obtenir cet objet GADVideoController
en appelant GADMediaContent.videoController
.
Rappels pour les événements vidéo
Pour gérer des événements vidéo spécifiques, vous devez écrire une classe qui implémente le protocole GADVideoControllerDelegate
. Les méthodes du protocole sont toutes facultatives.
L'exemple suivant montre comment mettre en œuvre le protocole délégué:
Swift
class ViewController: GADNativeAdLoaderDelegate, GADVideoControllerDelegate { private var adLoader: GADAdLoader? func viewDidLoad() { super.viewDidLoad() let videoOptions = GADVideoOptions() videoOptions.customControlsRequested = true adLoader = GADAdLoader( adUnitID: "ca-app-pub-3940256099942544/3986624511", rootViewController: self, adTypes: [GADAdLoaderAdTypeNative], options: [videoOptions]) adLoader?.delegate = self adLoader?.load(GADRequest()) } func adLoader( _ adLoader: GADAdLoader?, didReceive nativeAd: GADNativeAd? ) { // Set the videoController's delegate to be notified of video events. nativeAd?.mediaContent.videoController.delegate = self } // GADVideoControllerDelegate methods func videoControllerDidPlayVideo(_ videoController: GADVideoController) { // Implement this method to receive a notification when the video controller // begins playing the ad. } func videoControllerDidPauseVideo(_ videoController: GADVideoController) { // Implement this method to receive a notification when the video controller // pauses the ad. } func videoControllerDidEndVideoPlayback(_ videoController: GADVideoController) { // Implement this method to receive a notification when the video controller // stops playing the ad. } func videoControllerDidMuteVideo(_ videoController: GADVideoController) { // Implement this method to receive a notification when the video controller // mutes the ad. } func videoControllerDidUnmuteVideo(_ videoController: GADVideoController) { // Implement this method to receive a notification when the video controller // unmutes the ad. } }
Objective-C
@interface ViewController () <GADNativeAdLoaderDelegate, GADVideoControllerDelegate> @property(nonatomic, strong) GADAdLoader *adLoader; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init]; videoOptions.customControlsRequested = YES; self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511" rootViewController:self adTypes:@[ GADAdLoaderAdTypeNative ] options:@[ videoOptions ]]; self.adLoader.delegate = self; [self.adLoader loadRequest:[GAMRequest request]]; } - (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativeAd { // Set the videoController's delegate to be notified of video events. nativeAd.mediaContent.videoController.delegate = self; } // GADVideoControllerDelegate methods - (void)videoControllerDidPlayVideo:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // begins playing the ad. } - (void)videoControllerDidPauseVideo:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // pauses the ad. } - (void)videoControllerDidEndVideoPlayback:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // stops playing the ad. } - (void)videoControllerDidMuteVideo:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // mutes the ad. } - (void)videoControllerDidUnmuteVideo:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // unmutes the ad. } @end
Consultez les Règles et consignes relatives aux annonces natives pour en savoir plus sur l'affichage de vos annonces natives.