Video Ads

Prerequisites

Native ads, Instream ads and custom native template ads provide access to a GADMediaContent class that is used to get information about media content which could be a video or an image. It is also used to control the video ad playback listen for playback events. You can access the media content object via the .mediaContent property on the ad.

GADMediaContent

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

if(myNativeAd.mediaContent.hasVideoContent){

   CGFloat mediaAspectRatio = myNativeAd.mediaContent.aspectRatio;
   NSTimeInterval duration = myNativeAd.mediaContent.duration;
   ...
}

GADVideoController

The GADMediaContent object has a reference to a GADVideoController object. The GADVideoController object allows publishers to respond to video events. It also provides methods allowing you to control playback with Instream ads.

Ads served through line items can also be controlled with the GADVideoController.

This GADMediaContent object can be obtained by calling mediaContent.videoController.

Callbacks for video events

To handle specific video events you need to write a class that implements the GADVideoControllerDelegate protocol. The methods of the protocol are all optional.

The following example shows how to implement the delegate protocol:

@interface ViewController () <GADUnifiedNativeAdLoaderDelegate, GADVideoControllerDelegate>
@property(nonatomic, strong) GADAdLoader *adLoader;

@end

@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];

GADNativeAdViewAdOptions *adViewOptions = [[GADNativeAdViewAdOptions alloc] init];
adViewOptions.customControlsRequested = YES;
self.adLoader = [[GADAdLoader alloc]
        initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
        rootViewController:self
               adTypes:@[kGADAdLoaderAdTypeUnifiedNative]
               options:@[adViewOptions]];

        self.adLoader.delegate = self;
        [self.adLoader loadRequest:[DFPRequest request]];

}

- (void)adLoader:(GADAdLoader *)adLoader
    didReceiveUnifiedNativeAd:(GADUnifiedNativeAd *)nativeAd {
 
    // Set the videoController's delegate to be notified of video events.
    nativeAd.mediaContent.videoController.delegate = self;
}
 // GADVideoControllerDelegate methods
- (void)videoControllerDidPlayVideo:(nonnull GADVideoController *)videoController {
   //Implement this method to receive a notification when the video controller begins playing the ad.
}

- (void)videoControllerDidPauseVideo:(nonnull GADVideoController *)videoController {
 //Implement this method to receive a notification when the video controller pauses the ad.
}

- (void)videoControllerDidEndVideoPlayback:(nonnull GADVideoController *)videoController {
 //Implement this method to receive a notification when the video controller stops playing the ad.
}

- (void)videoControllerDidMuteVideo:(nonnull GADVideoController *)videoController {
  //Implement this method to receive a notification when the video controller mutes the ad.
}

- (void)videoControllerDidUnmuteVideo:(nonnull GADVideoController *)videoController {
 //Implement this method to receive a notification when the video controller unmutes the ad.
}

@end

Control playback

Instream Ads allow publishers to control playback by using certain methods on GADVideoController.

For reserved ads served through line items, you can also control playback through a native ad's GADVideoController. However, native ads require you to ask for custom control at request time by using GADVideoOptions. Once the ad is loaded, you can check if custom controls are enabled by using customControlsEnabled:

@interface ViewController () <GADUnifiedNativeAdLoaderDelegate, GADVideoControllerDelegate>
@property(nonatomic, strong) GADAdLoader *adLoader;

@end

@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];

GADNativeAdViewAdOptions *adViewOptions = [[GADNativeAdViewAdOptions alloc] init];
adViewOptions.customControlsRequested = YES;
self.adLoader = [[GADAdLoader alloc]
        initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
        rootViewController:self
               adTypes:@[kGADAdLoaderAdTypeUnifiedNative]
               options:@[adViewOptions]];

        self.adLoader.delegate = self;
        [self.adLoader loadRequest:[DFPRequest request]];

}

- (void)adLoader:(GADAdLoader *)adLoader
    didReceiveUnifiedNativeAd:(GADUnifiedNativeAd *)nativeAd {
   // Check if ad has video controls:
  if(nativeAd.mediaContent.videoController.customControlsEnabled) {
    // Custom controls are enabled for this ad.
  } else {
   // Custom controls are not enabled for this ad.
 }
}

@end

The GADVideoController methods that allow you to control video playback are:

GADVideoController videoController = myNativeAd.mediaContent.videoController;

if(videoController.customControlsEnabled) {
  // Pause the video.
  [videoController pause];

  // Play the video.
  [videoController play];

  // Stop the video.
  [videoController stop];

  // Mute the video.
  [videoController setMute:YES];

  // Unmute the video.
  [videoController setMute:NO];
}