MediaContent
原生廣告會提供MediaContent
物件存取權,用於取得媒體內容 (可能是影片或圖片) 的相關資訊。也用於控制影片廣告播放,並監聽播放事件。您可以呼叫 NativeAd.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()
...
}
影片事件的回呼
如要處理特定的影片事件,請編寫可擴充抽象 VideoLifecycleCallbacks
類別的類別,並在 VideoController
上呼叫 setVideoLifecycleCallbacks()
。然後只覆寫您在意的回呼。
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")
}
}