When displaying reserved native ads that include video assets, you can display your own video controls for play, pause, mute, and unmute. This guide outlines how to implement this feature in an Android app.
Request custom video controls
The first step is to request custom video controls. This is done using the
VideoOptions
class.
First, create an instance of the VideoOptions.Builder()
, and call
setCustomControlsRequested()
with a value of true
. Next, build an instance
of NativeAdOptions
using this set of video options, and then build an
AdLoader
with the
NativeAdOptions
instance.
Java
VideoOptions videoOptions = new VideoOptions.Builder() .setCustomControlsRequested(true) .build(); NativeAdOptions adOptions = new NativeAdOptions.Builder() .setVideoOptions(videoOptions) .build(); // Included below is the sample ad unit for testing. Be sure to replace with // your own ad unit when ready. AdLoader.Builder builder = new AdLoader.Builder(context, "/6499/example/native-video"); builder.withNativeAdOptions(adOptions); AdLoader adLoader = builder.build(); adLoader.loadAd(new AdManagerAdRequest.Builder().build());
Kotlin
val videoOptions = VideoOptions.Builder() .setCustomControlsRequested(true) .build() val adOptions = NativeAdOptions.Builder() .setVideoOptions(videoOptions) .build() // Included below is the sample ad unit for testing. Be sure to replace with // your own ad unit when ready. val adLoader = AdLoader.Builder(context,"/6499/example/native-video") .withNativeAdOptions(adOptions) .build() adLoader.loadAd(AdManagerAdRequest.Builder().build())
Check whether custom controls are enabled for an ad
Because it's not known at request time whether the returned ad will allow custom video controls, you must check whether it has custom controls enabled.
Java
@Override public void onNativeAdLoaded(NativeAd nativeAd) { MediaContent mediaContent = nativeAd.getMediaContent(); if (mediaContent != null) { VideoController videoController = mediaContent.getVideoController(); boolean canShowCustomControls = videoController.isCustomControlsEnabled(); } }
Kotlin
NativeAd.OnNativeAdLoadedListener { ad -> val mediaContent = ad.mediaContent if (mediaContent != null) { val videoController = mediaContent.videoController val canShowCustomControls = videoController.isCustomControlsEnabled } }
Implement custom controls
If the ad does have video content and custom controls are enabled, you should
then display your custom controls along with the ad, as the ad will not show any
controls itself. The controls can then call the relevant methods on the
VideoController
.
These methods are:
mute(boolean mute)
- Mute / unmute the videoplay()
- Play the videopause()
- Pause the videoisMuted()
- Check the mute state of the video controller
For an implementation example, check out the Google Ad Manager Custom Controls example in the API Demo in our sample apps.