네이티브 동영상 광고

기본 요건

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 객체를 사용하여 동영상 이벤트에 응답할 수 있습니다.

GADMediaContent.videoController를 호출하여 이 GADVideoController 객체를 가져올 수 있습니다.

동영상 이벤트 콜백

특정 동영상 이벤트를 처리하려면 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

네이티브 광고를 렌더링하는 방법은 네이티브 광고 정책 및 가이드라인을 참고하세요.