Inserting Ads Dynamically

Use the following steps to configure your app to insert ads dynamically into a video during playback.
  1. Use the steps outlined in Getting Started: Setting Up Your Environment in Xcode to create the project framework.
  2. Open the ViewController.m file for editing in Xcode.
  3. Add the following import statements:
    • #import <OoyalaSDK/OOVASTAdSpot.h>
    • #import <OoyalaSDK/OOManagedAdSpot.h>
    • #import <OoyalaSDK/OOManagedAdsPlugin.h>
    • #import <OoyalaSDK/OOOoyalaAdSpot.h>
    Note: <OoyalaSDK/OOVASTAdSpot.h> is only need for the VAST implementation of this tutorial and <OoyalaSDK/OOOyalaAdSpot.h> is only needed for the Ooyala Ad implementation of this tutorial.
  4. Add the insertAd functionality into the notificationHandler.
    Attention: insertAd must be called after OOOoyalaPlayerStateReady, otherwise the inserted ads are removed at runtime.
    • For a VAST ad, this step is described in the following code example:

      Important: In the following example, replace VastAdURL with the appropriate URL for you content.
      - (void) notificationHandler:(NSNotification*) notification {
        NSString* name = notification.name;
          if  (_ooyalaPlayerViewController.player.state == OOOoyalaPlayerStateReady) {
              [_ooyalaPlayerViewController.player.managedAdsPlugin insertAd: 
                 [[OOVASTAdSpot alloc]initWithTime:@0 clickURL:nil trackingURLs:nil vastURL:
              [NSURL URLWithString:@"VastAdUrl"]]]; }
      
    • For an Ooyala ad, this step is described in the following code example:

      Important: In the following example, replace OoyalaEmbedCode with the appropriate embed code for you content.
      - (void) notificationHandler:(NSNotification*) notification {
        NSString* name = notification.name;
          if  (_ooyalaPlayerViewController.player.state == OOOoyalaPlayerStateReady) {   
              [_ooyalaPlayerViewController.player.managedAdsPlugin insertAd: 
                [[OOOoyalaAdSpot alloc]initWithTime:@0 clickURL:nil trackingURLs:nil
                  embedCode:@"OoyalaEmbedCode"
                api:_ooyalaPlayerViewController.player.api]];}

Was this article helpful?