Skip to main content
Skip table of contents

Playback of linear adverts

In SwiftUI-based applications, code snippets shown in the ViewController class are implemented in the feature's model file. 

To enable playback of linear adverts.

  1. In ViewController.swift, add the following imports.

    CODE
    import OPYSDKFPS
    import GoogleInteractiveMediaAds
  2. Update the View Controller to specify the configuration of the required adverts in an ad tag URL, for example:

    CODE
    class ViewController: UIViewController {
     
     let otvPlayer : OTVAVPlayer
     @IBOutlet weak var playerView: PlayerView!
     let assetURL = URL(string:
       "https://d3bqrzf9w11pn3.cloudfront.net/basic_hls_bbb_clear/index.m3u8")!
     
     //VMAP Pre-, Mid-, and Post-rolls, Single Ads
     let adTagURL = "https://pubads.g.doubleclick.net/gampad/ads?sz=640x480" +
                   "&iu=/124319096/external/ad_rule_samples&ciu_szs=300x250&ad_rule=1&impl=s&" +
                   "gdfp_req=1&env=vp&output=vmap&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26" +
                   "sample_ar%3Dpremidpost&cmsid=496&vid=short_onecue&correlator=;"
     ...
  3. After the init method, add an attribute to access the IMAWrapper.

    CODE
    // IMAWrapper that is used to manage the IMA Google Framework
    var imaWrapper: IMAWrapper?
  4. In the same file, implement the IMAWrapperDelegate protocol. This allows the IMAWrapper class’s functionality to be triggered for advert management. Do this by extending the ViewController class, for example:

    CODE
    // ViewController must adopt the protocol IMAWrapperDelegate
    // so the IMAWrapper can access player fuctions.
    extension ViewController: IMAWrapperDelegate {
      func pauseContent() {
        print("IMAWrapper: pause video to show ads")
        otvPlayer.pause()
      }
     
      func resumeContent() {
        print("IMAWrapper: resume video")
        otvPlayer.play()
      }
     
      func allAdsCompleted() {
        print("IMAWrapper: all ads completed")
      }
     
      func log(event: String?) {
        print("IMAWrapper: AdsManager error: \(event ?? "empty message")")
      }
    }
  5. Add the viewDidAppear method, with the following:

    • Instantiate the IMAWrapperAdsSettings class to override default Google IMA configuration.

    • Instantiate the IMAWrapperPlayerDetails class which allows the provision of the UI elements and details of the Ad Tag URI to the IMAWrapper.

    • Instantiate the IMAWrapper class, providing a reference to the class that implements the IMAWrapperDelegate protocol. In this example, ViewController implements the protocol so we pass a reference to self.

    • Call requestAds() to start the ads.

    Your viewDidAppear method should look like the following:

    CODE
    override func viewDidAppear(_ animated: Bool) {
     super.viewDidAppear(animated)
     
     // Customise the settings of the IMAWrapper see API documention for details
     let setupSettings = IMAWrapperAdsSettings(settingsDictionary: [String: Any]() )
     
     // Set companionAdViews to nil if there are no companionAdViews
     let playerDetails = IMAWrapperPlayerDetails(contentPlayer: otvPlayer,
                                                     adsUIView: playerView,
                                                       adTagURL: adTagURL,
                                               companionAdViews: nil)
     
     // Initialise the IMAWrapper object
     imaWrapper = IMAWrapper(withPlayerDetails: playerDetails,
                                   withDelegate: self,
                                   withSettings: setupSettings)
     
     // Request ads to start, no need to call player.play() since the player will start once adverts complete
     // N.B. requestAds() will return false if the adTagURL hasn't been set/ is empty.
     if imaWrapper?.requestAds() == true {
       print("IMAWrapper: requestAds returned true")
     }
    }
  6. Build and run on a device to see the adverts.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.