原生视频广告

前提条件

GADMediaContent

您可以通过原生广告访问 GADMediaContent 类,该类用于获取有关媒体内容(可以是视频或图片)的信息。该类还可用于控制针对视频广告播放的监听播放事件。您可以通过广告的 .mediaContent 属性访问媒体内容对象。

GADMediaContent 对象包含视频的宽高比和时长等信息。以下代码段展示了如何获取原生广告的宽高比和时长。

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

GADMediaContent 对象引用了 GADVideoController 对象。GADVideoController 对象允许发布商响应视频事件。

GADVideoController 对象可通过调用 GADMediaContent.videoController 获取。

视频事件的回调

如需处理特定视频事件,您需要编写一个实现 GADVideoControllerDelegate 协议的类。该协议的所有方法都是可选的。

以下示例展示了如何实现委托协议:

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

有关如何呈现原生广告的更多指导,请参阅原生广告政策和指南