Annunci video nativi

Prerequisiti

GADMediaContent

Gli annunci nativi consentono di accedere a una classe GADMediaContent che viene utilizzata per ottenere informazioni sui contenuti multimediali, che possono essere un video o un'immagine. Viene utilizzato anche per controllare l'ascolto della riproduzione degli annunci video per gli eventi di riproduzione. Puoi accedere all'oggetto contenuto multimediale tramite la proprietà .mediaContent nell'annuncio.

L'oggetto GADMediaContent contiene informazioni come le proporzioni e la durata del video. Lo snippet riportato di seguito mostra come ottenere le proporzioni e la durata di un annuncio 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

L'oggetto GADMediaContent ha un riferimento a un oggetto GADVideoController. L'oggetto GADVideoController consente ai publisher di rispondere agli eventi video.

Questo oggetto GADVideoController può essere ottenuto chiamando GADMediaContent.videoController.

Callback per gli eventi video

Per gestire eventi video specifici, devi scrivere una classe che implementi il protocollo GADVideoControllerDelegate. I metodi del protocollo sono tutti facoltativi.

L'esempio seguente mostra come implementare il protocollo delegato:

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:[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

Leggi le norme e le linee guida sugli annunci nativi per avere ulteriori indicazioni su come eseguire il rendering degli annunci nativi.