前提条件
- ネイティブ広告フォーマットを組み込みます。
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
ネイティブ広告のポリシーと ガイドラインを確認し、 ネイティブ広告をレンダリングできます