Native video ads

  • The MediaContent object provides information about native ad media content, including video and image details.

  • You can obtain the MediaContent object by calling NativeAd.getMediaContent() .

  • The MediaContent object can be used to get the aspect ratio and duration of a video ad.

  • Video playback can be controlled and video events can be handled by setting VideoLifecycleCallbacks on the VideoController obtained from the MediaContent object.

Select platform: Android (beta)New Android iOS

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

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

Java

  if 
  
 ( 
 nativeAd 
 . 
 getMediaContent 
 () 
  
 != 
  
 null 
 ) 
  
 { 
  
 MediaContent 
  
 mediaContent 
  
 = 
  
 nativeAd 
 . 
 getMediaContent 
 (); 
  
 float 
  
 mediaAspectRatio 
  
 = 
  
 mediaContent 
 . 
 getAspectRatio 
 (); 
  
 if 
  
 ( 
 mediaContent 
 . 
 hasVideoContent 
 ()) 
  
 { 
  
 float 
  
 duration 
  
 = 
  
 mediaContent 
 . 
 getDuration 
 (); 
  
 } 
 } 
  
 

Kotlin

  nativeAd 
 . 
 mediaContent 
 ?. 
 let 
  
 { 
  
 mediaContent 
  
 - 
>  
 val 
  
 mediaAspectRatio 
 : 
  
 Float 
  
 = 
  
 mediaContent 
 . 
 aspectRatio 
  
 if 
  
 ( 
 mediaContent 
 . 
 hasVideoContent 
 ()) 
  
 { 
  
 val 
  
 duration 
 : 
  
 Float 
  
 = 
  
 mediaContent 
 . 
 duration 
  
 } 
 } 
  
 

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

  if 
  
 ( 
 nativeAd 
 . 
 getMediaContent 
 () 
  
 != 
  
 null 
 ) 
  
 { 
  
 VideoController 
  
 videoController 
  
 = 
  
 nativeAd 
 . 
 getMediaContent 
 (). 
 getVideoController 
 (); 
  
 if 
  
 ( 
 videoController 
  
 != 
  
 null 
 ) 
  
 { 
  
 videoController 
 . 
 setVideoLifecycleCallbacks 
 ( 
  
 new 
  
 VideoController 
 . 
 VideoLifecycleCallbacks 
 () 
  
 { 
  
 @Override 
  
 public 
  
 void 
  
 onVideoStart 
 () 
  
 { 
  
 Log 
 . 
 d 
 ( 
 TAG 
 , 
  
 "Video started." 
 ); 
  
 } 
  
 @Override 
  
 public 
  
 void 
  
 onVideoPlay 
 () 
  
 { 
  
 Log 
 . 
 d 
 ( 
 TAG 
 , 
  
 "Video played." 
 ); 
  
 } 
  
 @Override 
  
 public 
  
 void 
  
 onVideoPause 
 () 
  
 { 
  
 Log 
 . 
 d 
 ( 
 TAG 
 , 
  
 "Video paused." 
 ); 
  
 } 
  
 @Override 
  
 public 
  
 void 
  
 onVideoEnd 
 () 
  
 { 
  
 Log 
 . 
 d 
 ( 
 TAG 
 , 
  
 "Video ended." 
 ); 
  
 } 
  
 @Override 
  
 public 
  
 void 
  
 onVideoMute 
 ( 
 boolean 
  
 isMuted 
 ) 
  
 { 
  
 Log 
 . 
 d 
 ( 
 TAG 
 , 
  
 "Video isMuted: " 
  
 + 
  
 isMuted 
  
 + 
  
 "." 
 ); 
  
 } 
  
 }); 
  
 } 
 } 
  
 

Kotlin

  val 
  
 videoLifecycleCallbacks 
  
 = 
  
 object 
  
 : 
  
 VideoController 
 . 
 VideoLifecycleCallbacks 
 () 
  
 { 
  
 override 
  
 fun 
  
 onVideoStart 
 () 
  
 { 
  
 Log 
 . 
 d 
 ( 
 TAG 
 , 
  
 "Video started." 
 ) 
  
 } 
  
 override 
  
 fun 
  
 onVideoPlay 
 () 
  
 { 
  
 Log 
 . 
 d 
 ( 
 TAG 
 , 
  
 "Video played." 
 ) 
  
 } 
  
 override 
  
 fun 
  
 onVideoPause 
 () 
  
 { 
  
 Log 
 . 
 d 
 ( 
 TAG 
 , 
  
 "Video paused." 
 ) 
  
 } 
  
 override 
  
 fun 
  
 onVideoEnd 
 () 
  
 { 
  
 Log 
 . 
 d 
 ( 
 TAG 
 , 
  
 "Video ended." 
 ) 
  
 } 
  
 override 
  
 fun 
  
 onVideoMute 
 ( 
 isMuted 
 : 
  
 Boolean 
 ) 
  
 { 
  
 Log 
 . 
 d 
 ( 
 TAG 
 , 
  
 "Video isMuted: 
 $ 
 isMuted 
 ." 
 ) 
  
 } 
  
 } 
 nativeAd 
 . 
 mediaContent 
 ?. 
 videoController 
 ?. 
 videoLifecycleCallbacks 
  
 = 
  
 videoLifecycleCallbacks  
 
 . 
 kt 
 
Design a Mobile Site
View Site in Mobile | Classic
Share by: