To enable playback of linear adverts.
-
In
ViewController.swift, add the following imports.import OPYSDKFPS import GoogleInteractiveMediaAds -
Update the View Controller to specify the configuration of the required adverts in an ad tag URL, for example:
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=;" ... -
After the
initmethod, add an attribute to access theIMAWrapper.// IMAWrapper that is used to manage the IMA Google Framework var imaWrapper: IMAWrapper? -
In the same file, implement the
IMAWrapperDelegateprotocol. This allows theIMAWrapperclass’s functionality to be triggered for advert management. Do this by extending theViewControllerclass, for example:// 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")") } } -
Add the
viewDidAppearmethod, with the following:-
Instantiate the
IMAWrapperAdsSettingsclass to override default Google IMA configuration. -
Instantiate the
IMAWrapperPlayerDetailsclass which allows the provision of the UI elements and details of the Ad Tag URI to theIMAWrapper. -
Instantiate the
IMAWrapperclass, providing a reference to the class that implements theIMAWrapperDelegateprotocol. In this example,ViewControllerimplements the protocol so we pass a reference to self. -
Call
requestAds()to start the ads.
Your
viewDidAppearmethod should look like the following: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") } } -
-
Build and run on a device to see the adverts.