โฆษณาวิดีโอเนทีฟ

MediaContent

โฆษณาเนทีฟและรูปแบบโฆษณาเนทีฟที่กำหนดเอง ให้สิทธิ์เข้าถึง MediaContent ที่ใช้สำหรับรับข้อมูลเกี่ยวกับเนื้อหาสื่อ ซึ่งอาจเป็น วิดีโอหรือรูปภาพ นอกจากนี้ยังใช้เพื่อควบคุมการเล่นและฟังโฆษณาวิดีโอ เหตุการณ์การเล่น คุณสามารถรับออบเจ็กต์ MediaContent ได้โดยเรียกใช้ NativeAd.getMediaContent()

ออบเจ็กต์ 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()
  ...
}

การติดต่อกลับสำหรับเหตุการณ์วิดีโอ

เขียนชั้นเรียนที่ขยายบทคัดย่อเพื่อจัดการเหตุการณ์วิดีโอที่เฉพาะเจาะจง VideoLifecycleCallbacks ชั้นเรียน และโทร setVideoLifecycleCallbacks() ในวันที่ VideoController จากนั้นลบล้างเฉพาะ Callback ที่คุณสนใจเท่านั้น

Java

myNativeAd.getMediaContent().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

myNativeAd.getMediaContent().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")
  }
}