前提条件
- 集成原生广告格式。
媒体内容
原生广告 提供对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() ... }
视频选项
借助 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") .forNativeAd( ... ) .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") .forNativeAd( ... ) .withNativeAdOptions(adOptions) .build()
视频事件的回调
如需处理特定的视频事件,请编写一个扩展抽象 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") } }