Anuncios de video nativos

Selecciona la plataforma: Android iOS

Requisitos previos

GADMediaContent

Los anuncios nativos proporcionan acceso a una clase GADMediaContent que se usa para obtener información sobre el contenido multimedia, que puede ser un video o una imagen. También se usa para controlar la reproducción de anuncios de video y escuchar eventos de reproducción. Puedes acceder al objeto de contenido multimedia a través de la propiedad .mediaContent del anuncio.

El objeto GADMediaContent contiene información como la relación de aspecto y la duración del video. En el siguiente fragmento, se muestra cómo obtener la relación de aspecto y la duración de un anuncio nativo.

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

El objeto GADMediaContent tiene una referencia a un objeto GADVideoController. El objeto GADVideoController permite a los publicadores responder a eventos de video.

Este objeto GADVideoController se puede obtener llamando a GADMediaContent.videoController.

Devoluciones de llamada para eventos de video

Para controlar eventos de video específicos, debes escribir una clase que implemente el protocolo GADVideoControllerDelegate. Todos los métodos del protocolo son opcionales.

En el siguiente ejemplo, se muestra cómo implementar el protocolo de delegado:

Swift

class ViewController: NativeAdLoaderDelegate, VideoControllerDelegate {
  private var adLoader: AdLoader?

  func viewDidLoad() {
    super.viewDidLoad()

    let videoOptions = VideoOptions()
    videoOptions.customControlsRequested = true
    adLoader = AdLoader(
        adUnitID: "ca-app-pub-3940256099942544/3986624511",
        rootViewController: self,
        adTypes: [.native],
        options: [videoOptions])
    adLoader?.delegate = self
    adLoader?.load(Request())

  }

  func adLoader(
      _ adLoader: AdLoader?,
      didReceive nativeAd: NativeAd?
  ) {
    // Set the videoController's delegate to be notified of video events.
    nativeAd?.mediaContent.videoController.delegate = self
  }

  // VideoControllerDelegate methods
  func videoControllerDidPlayVideo(_ videoController: VideoController) {
    // Implement this method to receive a notification when the video controller
    // begins playing the ad.
  }

  func videoControllerDidPauseVideo(_ videoController: VideoController) {
    // Implement this method to receive a notification when the video controller
    // pauses the ad.
  }

  func videoControllerDidEndVideoPlayback(_ videoController: VideoController) {
    // Implement this method to receive a notification when the video controller
    // stops playing the ad.
  }

  func videoControllerDidMuteVideo(_ videoController: VideoController) {
    // Implement this method to receive a notification when the video controller
    // mutes the ad.
  }

  func videoControllerDidUnmuteVideo(_ videoController: VideoController) {
    // 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:[GADRequest 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

Lee los lineamientos y políticas de los anuncios nativos para obtener más orientación sobre cómo renderizar tus anuncios nativos.