前提条件
- 集成原生广告格式。
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
进行控制。
此 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:[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
请阅读原生广告政策 指南,详细了解如何 来呈现您的原生广告