Requisitos previos
- Importar el SDK de anuncios de Google para móviles, ya sea de forma independiente o como parte de Firebase.
- Integrar el formato de anuncio nativo.
MediaContent
Los anuncios nativos permiten acceder a un objetoMediaContent
que se usa para obtener información sobre el contenido multimedia, que puede ser un vídeo o una imagen. También se usa para controlar la reproducción de los anuncios de vídeo y procesar eventos de reproducción. Para obtener el objeto MediaContent
, llama a myAd.getMediaContent()
.
El objeto MediaContent
contiene información como la relación de aspecto y la duración de un vídeo. En el siguiente fragmento se muestra cómo obtener la relación de aspecto y la duración de un anuncio nativo.
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
La clase VideoOptions
permite que las aplicaciones configuren el comportamiento de los recursos de vídeo nativos.
Los objetos VideoOptions
deben asignarse a un objeto NativeAdOptions
que se utiliza al construir el AdLoader
:
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()
Retrollamadas de eventos de vídeo
Para gestionar eventos de vídeo concretos, escribe una clase que extienda la VideoLifecycleCallbacks
abstracta y llama a setVideoLifecycleCallbacks()
en el VideoController
. Después, anula solo las retrollamadas que te interesen.
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") } }