原生视频广告

前提条件

MediaContent

借助 原生广告,您可以访问 MediaContent 对象,该对象用于获取与媒体内容(可以是视频或者图片)相关的信息。该对象也可用于控制视频广告播放和监听播放事件。您可以通过调用 myAd.getMediaContent() 获取 MediaContent 对象。

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

Java

if (myNativeAd.getMediaContent().hasVideoContent()) {

   float mediaAspectRatio = myNativeAd.getMediaContent().getAspectRatio();
   float duration = myNativeAd.getMediaContent().getDuration();
   ...
}

Kotlin

if (myNativeAd.getMediaContent().hasVideoContent()) {

   val mediaAspectRatio: Float = myNativeAd.getMediaContent().getAspectRatio()
   val duration: Float = myNativeAd.getMediaContent().getDuration()
   ...
}

VideoOptions

通过 VideoOptions 类,您可以为应用配置原生视频素材资源的行为方式。VideoOptions 对象应该分配给构建 AdLoader 时使用的 NativeAdOptions 对象:

Java

VideoOptions videoOptions = new VideoOptions.Builder()
        .setStartMuted(false)
        .build();

NativeAdOptions adOptions = new NativeAdOptions.Builder()
        .setVideoOptions(videoOptions)
        .build();

AdLoader adLoader = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
        .forUnifiedNativeAd( ... )
        .withNativeAdOptions(adOptions)
        .build();

Kotlin

val videoOptions = VideoOptions.Builder()
        .setStartMuted(false)
        .build()

val adOptions = NativeAdOptions.Builder()
        .setVideoOptions(videoOptions)
        .build()
val adLoader = AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
        .forUnifiedNativeAd( ... )
        .withNativeAdOptions(adOptions)
        .build()

视频事件的回调

要处理特定视频事件,请写入用于扩展 VideoLifecycleCallbacks 抽象类的类,并对 VideoController 调用 setVideoLifecycleCallbacks()。然后,仅替换所需的回调。

Java

mediaContent.getVideoController()
        .setVideoLifecycleCallbacks(new VideoLifecycleCallbacks() {

  /** Called when video playback first begins. */
  @Override
  public void onVideoStart() {
    // Do something when the video starts the first time.
    Log.d("MyApp", "Video Started");
  }

  /** Called when video playback is playing. */
  @Override
  public void onVideoPlay() {
    // Do something when the video plays.
    Log.d("MyApp", "Video Played");
  }

  /** Called when video playback is paused. */
  @Override
  public void onVideoPause() {
    // Do something when the video pauses.
    Log.d("MyApp", "Video Paused");
  }

  /** Called when video playback finishes playing. */
  @Override
  public void onVideoEnd() {
    // Do something when the video ends.
    Log.d("MyApp", "Video Ended");
  }

  /** Called when the video changes mute state. */
  @Override
  public void onVideoMute(boolean isMuted) {
    // Do something when the video is muted.
    Log.d("MyApp", "Video Muted");
  }

});

Kotlin

mediaContent.getVideoController().setVideoLifecycleCallbacks {

  /** Called when video playback first begins. */
  override fun onVideoStart() {
    // Do something when the video starts the first time.
    Log.d("MyApp", "Video Started")
  }

  /** Called when video playback is playing. */
  override fun onVideoPlay() {
    // Do something when the video plays.
    Log.d("MyApp", "Video Played")
  }

  /** Called when video playback is paused. */
  override fun onVideoPause() {
    // Do something when the video pauses.
    Log.d("MyApp", "Video Paused")
  }

  /** Called when video playback finishes playing. */
  override fun onVideoEnd() {
    // Do something when the video ends.
    Log.d("MyApp", "Video Ended")
  }

  /** Called when the video changes mute state. */
  override fun onVideoMute(boolean isMuted) {
    // Do something when the video is muted.
    Log.d("MyApp", "Video Muted")
  }
}