Enable Google DAI playback

Enable ad playback with the Google Dynamic Ad Insertion ad client in an iOS app.


After adding the Google IMA Dynamic Ad Insertion (DAI) SDK to your app and acquiring the required items listed in the Requirements section, you can enable Google DAI ad playback in your iOS app.



Requirements

PropertyDescription
apiKey NSStringStream request API key
assetKey NSStringStream asset key, used for live streams

You can find this ID in your Google Ad Manager portal. Ask your Google representative for assistance locating this ID.
cmsID NSStringContent management system ID of the video, used for video on demand

You can find this ID in your Google Ad Manager portal. Ask your Google representative for assistance locating this ID.
videoID NSStringIdentifier of the DAI video to be displayed, used for video on demand

You can find this ID in your Google Ad Manager portal. Ask your Google representative for assistance locating this ID.


Specify ad content for a single playlist item

Use the following steps to set up dynamic ad insertion for a single playlist item.

👍

IMPORTANT

For the following reasons, be sure that the video URL (videoURL in our code examples) used to set up the player is consistent with the media content registered with DAI for your videoID or assetKey:

• If the DAI request fails, the video URL will play as a fallback.
• Analytics will be attributed to the correct media item.

  1. Define your Google account information.
    • If you are displaying a video on demand, define cmsID and videoID.
    • If you are displaying a live stream, define assetKey.

  2. Instantiate a JWAdvertisingConfig object using JWImaDaiAdvertisingConfigBuilder() and the JWGoogleDAIStream object.
    • If you are displaying a video on demand, use vodStreamInfo(videoID:cmsID:).
    • If you are displaying a live stream, use liveStreamInfo(assetKey:).

  3. If your content is protected, set the apiKey property with your Google DAI API key.
  4. Assign the adConfig to your JWPlayerConfiguration.

In the code examples below, we have added a DAI configuration for a video-on-demand playlist item.



Video On Demand

import JWPlayerKit

class ViewController: JWPlayerViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        do {
            // Create the DAI advertising configuration
            let stream = JWGoogleDAIStreamBuilder().apiKey(<#API Key#>)
                .vodStreamInfo(videoID: <#Video ID#>, cmsID: <#CMS ID#>)
                .build()
            let adConfig = JWImaDaiAdvertisingConfigBuilder()
                .googleDAIStream(stream)
                .build()

            // Create the content to be played.
            let item = try JWPlayerItemBuilder()
                .file(URL(string:<#Fallback Video URL#>)!)
                .build()

            // Create a config, and give it the item as a playlist.
            // Set it to begin automatically.
            let config = try JWPlayerConfigurationBuilder()
                .playlist([item])
                .autostart(true)
                .advertising(adConfig)
                .build()

            // Set the config.
            player.configurePlayer(with: config)
        }
        catch {
            // Handle Error
        }
    }
}
#import <JWPlayerKit/JWPlayerKit-Swift.h>
#import <JWPlayerKit/JWPlayerObjCViewController.h>

@interface ViewController : JWPlayerObjCViewController

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // For brevity, we are not checking for errors.
    // To check for errors, supply an NSError object to buildAndReturn.

    // Create the Google DAI Advertising configuration
    JWGoogleDAIStream *stream = [[[JWGoogleDAIStreamBuilder new]
                                  vodStreamInfoWithVideoID:<#Video ID#> cmsID:<#CMS ID#>]
                                 buildAndReturnError:nil];
    JWAdvertisingConfig *adConfig = [[[JWImaDaiAdvertisingConfigBuilder new]
                                      googleDAIStream:stream]
                                     buildAndReturnError:nil];

    // Create the content to be played.
    JWPlayerItem *item = [[[JWPlayerItemBuilder new]
                           file:<#Video URL#>]
                          buildAndReturnError:nil];

    // Create a config, and give it the item as a playlist.
    // Set it to begin automatically.
    JWPlayerConfiguration *config = [[[[[JWPlayerConfigurationBuilder new]
                                        playlist:@[item]]
                                       autostart:YES]
                                      advertising:adConfig]
                                     buildAndReturnError:nil];

    // Set the config
    [self.player configurePlayerWith:config];
}

@end

Live Stream

import JWPlayerKit

class ViewController: JWPlayerViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        do {
            // Create the DAI advertising configuration
            let stream = JWGoogleDAIStreamBuilder().apiKey(<#API Key#>)
                .liveStreamInfo(assetKey: <#Asset Key#>)
                .build()
            let adConfig = JWImaDaiAdvertisingConfigBuilder()
                .googleDAIStream(stream)
                .build()

            // Create the content to be played.
            let item = try JWPlayerItemBuilder()
                .file(URL(string:<#Fallback Video URL#>)!)
                .build()

            // Create a config, and give it the item as a playlist.
            // Set it to begin automatically.
            let config = try JWPlayerConfigurationBuilder()
                .playlist([item])
                .autostart(true)
                .advertising(adConfig)
                .build()

            // Set the config.
            player.configurePlayer(with: config)
        }
        catch {
            // Handle Error
        }
    }
}
#import <JWPlayerKit/JWPlayerKit-Swift.h>
#import <JWPlayerKit/JWPlayerObjCViewController.h>

@interface ViewController : JWPlayerObjCViewController

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // For brevity, we are not checking for errors.
    // To check for errors, supply an NSError object to buildAndReturn.

    // Create the Google DAI Advertising configuration
    JWGoogleDAIStream *stream = [[[JWGoogleDAIStreamBuilder new]
                                  liveStreamInfoWithAssetKey:<#Asset Key#>]
                                 buildAndReturnError:nil];
    JWAdvertisingConfig *adConfig = [[[JWImaDaiAdvertisingConfigBuilder new]
                                      googleDAIStream:stream]
                                     buildAndReturnError:nil];

    // Create the content to be played.
    JWPlayerItem *item = [[[JWPlayerItemBuilder new]
                           file:<#Video URL#>]
                          buildAndReturnError:nil];

    // Create a config, and give it the item as a playlist.
    // Set it to begin automatically.
    JWPlayerConfiguration *config = [[[[[JWPlayerConfigurationBuilder new]
                                        playlist:@[item]]
                                       autostart:YES]
                                      advertising:adConfig]
                                     buildAndReturnError:nil];

    // Set the config
    [self.player configurePlayerWith:config];
}

@end


Specify ad content for multiple playlist items

Use the following steps to associate new DAI ad content with another playlist item. You should follow the steps in the previous section first.

  1. Define your Google account information for each stream.
    • If you are displaying a video on demand, define cmsID and videoID.
    • If you are displaying a live stream, define assetKey.

  2. Instantiate a JWGoogleDAIStream for each JWPlayerItem in your playlist. In our code example, we name this daiConfig. This will serve as a default stream if the others cannot be loaded.
    • If you are displaying a video on demand, use vodStreamInfo(videoID:cmsID:).
    • If you are displaying a live stream, use liveStreamInfo(assetKey:).
    • If your content is protected, set the apiKey property with your Google DAI API key.

  3. Instantiate a JWAdvertisingConfig object using the JWImaDaiAdvertisingConfigBuilder() and the JWGoogleDAIStream object. This will serve as a default stream if the others cannot be loaded.
    • If you are displaying a video on demand, use vodStreamInfo(videoID:cmsID).
    • If you are displaying a live stream, use liveStreamInfo(assetKey:).

  4. Assign the adConfig to your JWPlayerConfiguration.

Video On Demand

#import <JWPlayerKit/JWPlayerKit-Swift.h>
#import <JWPlayerKit/JWPlayerObjCViewController.h>

@interface ViewController : JWPlayerObjCViewController

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // For brevity, we are not checking for errors.
    // To check for errors, supply an NSError object to buildAndReturn.

    // Create the DAI streams for use in the JWPlayerItems
    JWGoogleDAIStream *stream0 = [[[JWGoogleDAIStreamBuilder new]
                                   vodStreamInfoWithVideoID:<#Video ID#> cmsID:<#CMS ID#>]
                                  buildAndReturnError:nil];
    JWGoogleDAIStream *stream1 = [[[JWGoogleDAIStreamBuilder new]
                                   vodStreamInfoWithVideoID:<#Video ID#> cmsID:<#CMS ID#>]
                                  buildAndReturnError:nil];

    // Create the default Google DAI Advertising configuration
    JWGoogleDAIStream *defaultStream = [[[JWGoogleDAIStreamBuilder new]
                                         vodStreamInfoWithVideoID:<#Video ID#> cmsID:<#CMS ID#>]
                                        buildAndReturnError:nil];
    JWAdvertisingConfig *adConfig = [[[JWImaDaiAdvertisingConfigBuilder new]
                                      googleDAIStream:defaultStream]
                                     buildAndReturnError:nil];

    // Create the content to be played.
    JWPlayerItem *item0 = [[[[JWPlayerItemBuilder new]
                             file:<#Video URL#>]
                            googleDAIStream: stream0]
                           buildAndReturnError:nil];
    JWPlayerItem *item1 = [[[[JWPlayerItemBuilder new]
                             file:<#Video URL#>]
                            googleDAIStream: stream0]
                           buildAndReturnError:nil];

    // Create a config, and give it the item as a playlist.
    // Set it to begin automatically.
    JWPlayerConfiguration *config = [[[[[JWPlayerConfigurationBuilder new]
                                        playlist:@[item0, item1]]
                                       autostart:YES]
                                      advertising:adConfig]
                                     buildAndReturnError:nil];

    // Set the config
    [self.player configurePlayerWith:config];
}

@end
import JWPlayerKit

class ViewController: JWPlayerViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        do {
            // Create the DAI streams
            let stream0 = JWGoogleDAIStreamBuilder()
                .apiKey(<#API Key#>)
                .vodStreamInfo(videoID: <#Video ID#>, cmsID: <#CMS ID#>)
                .build()
            let stream1 = JWGoogleDAIStreamBuilder()
                .apiKey(<#API Key#>)
                .vodStreamInfo(videoID: <#Video ID#>, cmsID: <#CMS ID#>)
                .build()

            let defaultStream = JWGoogleDAIStreamBuilder()
                .apiKey(<#API Key#>)
                .vodStreamInfo(videoID: <#Video ID#>, cmsID: <#CMS ID#>)
                .build()
            let adConfig = JWImaDaiAdvertisingConfigBuilder()
                .googleDAIStream(defaultStream)
                .build()

            // Create the content to be played.
            let item0 = try JWPlayerItemBuilder()
                .file(URL(string:<#Fallback Video URL#>)!)
                .googleDAIStream(stream0)
                .build()
            let item1 = try JWPlayerItemBuilder()
                .file(URL(string:<#Fallback Video URL#>)!)
                .googleDAIStream(stream1)
                .build()

            // Create a config, and give it the item as a playlist.
            // Set it to begin automatically.
            let config = try JWPlayerConfigurationBuilder()
                .playlist([item0, item1])
                .autostart(true)
                .advertising(adConfig)
                .build()

            // Set the config.
            player.configurePlayer(with: config)
        }
        catch {
            // Handle Error
        }
    }
}

Live Stream

#import <JWPlayerKit/JWPlayerKit-Swift.h>
#import <JWPlayerKit/JWPlayerObjCViewController.h>

@interface ViewController : JWPlayerObjCViewController

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // For brevity, we are not checking for errors.
    // To check for errors, supply an NSError object to buildAndReturn.

    // Create the DAI streams for use in the JWPlayerItems
    JWGoogleDAIStream *stream0 = [[[JWGoogleDAIStreamBuilder new]
                                   liveStreamInfoWithAssetKey:<#Asset Key#>]
                                  buildAndReturnError:nil];
    JWGoogleDAIStream *stream1 = [[[JWGoogleDAIStreamBuilder new]
                                   liveStreamInfoWithAssetKey:<#Asset Key#>]
                                  buildAndReturnError:nil];

    // Create the default Google DAI Advertising configuration
    JWGoogleDAIStream *defaultStream = [[[JWGoogleDAIStreamBuilder new]
                                         liveStreamInfoWithAssetKey:<#Asset Key#>]
                                        buildAndReturnError:nil];
    JWAdvertisingConfig *adConfig = [[[JWImaDaiAdvertisingConfigBuilder new]
                                      googleDAIStream:defaultStream]
                                     buildAndReturnError:nil];

    // Create the content to be played.
    JWPlayerItem *item0 = [[[[JWPlayerItemBuilder new]
                             file:<#Video URL#>]
                            googleDAIStream: stream0]
                           buildAndReturnError:nil];
    JWPlayerItem *item1 = [[[[JWPlayerItemBuilder new]
                             file:<#Video URL#>]
                            googleDAIStream: stream0]
                           buildAndReturnError:nil];

    // Create a config, and give it the item as a playlist.
    // Set it to begin automatically.
    JWPlayerConfiguration *config = [[[[[JWPlayerConfigurationBuilder new]
                                        playlist:@[item0, item1]]
                                       autostart:YES]
                                      advertising:adConfig]
                                     buildAndReturnError:nil];

    // Set the config
    [self.player configurePlayerWith:config];
}

@end
import JWPlayerKit

class ViewController: JWPlayerViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        do {
            // Create the DAI streams
            let stream0 = JWGoogleDAIStreamBuilder()
                .apiKey(<#API Key#>)
                .liveStreamInfo(assetKey: <#Asset Key#>)
                .build()
            let stream1 = JWGoogleDAIStreamBuilder()
                .apiKey(<#API Key#>)
                .liveStreamInfo(assetKey: <#Asset Key#>)
                .build()

            let defaultStream = JWGoogleDAIStreamBuilder()
                .apiKey(<#API Key#>)
                .liveStreamInfo(assetKey: <#Asset Key#>)
                .build()
            let adConfig = JWImaDaiAdvertisingConfigBuilder()
                .googleDAIStream(defaultStream)
                .build()

            // Create the content to be played.
            let item0 = try JWPlayerItemBuilder()
                .file(URL(string:<#Fallback Video URL#>)!)
                .googleDAIStream(stream0)
                .build()
            let item1 = try JWPlayerItemBuilder()
                .file(URL(string:<#Fallback Video URL#>)!)
                .googleDAIStream(stream1)
                .build()

            // Create a config, and give it the item as a playlist.
            // Set it to begin automatically.
            let config = try JWPlayerConfigurationBuilder()
                .playlist([item0, item1])
                .autostart(true)
                .advertising(adConfig)
                .build()

            // Set the config.
            player.configurePlayer(with: config)
        }
        catch {
            // Handle Error
        }
    }
}


FAQ

Which iOS SDK features are not supported with Google DAI?



Did this page help you?