Native video ads

Prerequisites

MediaContent

Native ads and custom native ad formats provide access to a MediaContent object that is used to get information about media content, which could be video or an image. It is also used to control video ad playback and listen for playback events. You can obtain the MediaContent object by calling myAd.getMediaContent().

The MediaContent object contains information such as the aspect ratio and duration of a video. The snippet below shows how to get the aspect ratio and duration of a native ad.

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

The VideoOptions class allows apps to configure how native video assets should behave. VideoOptions objects should be assigned to a NativeAdOptions object that's used when constructing the AdLoader:

Java

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

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

AdLoader adLoader = new AdLoader.Builder(this, "/6499/example/native")
        .forUnifiedNativeAd( ... )
        .withNativeAdOptions(adOptions)
        .build();

Kotlin

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

val adOptions = NativeAdOptions.Builder()
        .setVideoOptions(videoOptions)
        .build()
val adLoader = AdLoader.Builder(this, "/6499/example/native")
        .forUnifiedNativeAd( ... )
        .withNativeAdOptions(adOptions)
        .build()

Callbacks for video events

To handle specific video events, write a class that extends the abstract VideoLifecycleCallbacks class, and call setVideoLifecycleCallbacks() on the VideoController. Then, override only the callbacks you care about.

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")
  }
}