1- // Copyright 2025 Google LLC. All rights reserved. 
1+ // Copyright 2025 Google LLC
22//
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
36//
4- // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
5- // file except in compliance with the License. You may obtain a copy of the License at
7+ //     https://www.apache.org/licenses/LICENSE-2.0
68//
7- //     http://www.apache.org/licenses/LICENSE-2.0
8- //
9- // Unless required by applicable law or agreed to in writing, software distributed under
10- // the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11- // ANY KIND, either express or implied. See the License for the specific language governing
12- // permissions and limitations under the License.
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
1314
1415import  AVFoundation
1516import  GoogleInteractiveMediaAds
@@ -21,33 +22,43 @@ class ViewController:
2122{ 
2223
2324  private  enum  StreamParameters  { 
24-     static  let  contentStream  =  " [YOUR_LIVE_STREAM_URL] " 
25+     static  let  contentStream  = 
26+       " https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_16x9/bipbop_16x9_variant.m3u8 " 
27+ 
2528    // Find your [Google Ad Manager network code](https://support.google.com/admanager/answer/7674889)
2629    // or use the test network code and custom asset key with the DAI type "Pod serving manifest"
2730    // from [DAI sample streams](https://developers.google.com/ad-manager/dynamic-ad-insertion/streams#pod_serving_dai).
28-     static  let  networkCode  =  " [YOUR_GOOGLE_AD_MANAGER_NETWORK_CODE] " 
29-     static  let  customAssetKey  =  " [YOUR_GOOGLE_DAI_CUSTOM_ASSET_KEY] " 
31+ 
32+     /// Google Ad Manager network code.
33+     static  let  networkCode  =  " 21775744923 " 
34+ 
35+     /// Google DAI livestream custom asset key.
36+     static  let  customAssetKey  =  " sgai-hls-live " 
37+ 
3038    // Set your ad break duration.
3139    static  let  adBreakDurationMs  =  10000 
3240  } 
3341
34-   /// The AVPlayer instance that plays the content and the ads.
35-   private  var  player :  AVPlayer ! 
36- 
3742  /// The play button to start the stream.
3843  /// It is hidden when the stream starts playing.
3944  @IBOutlet   private  weak var  playButton :  UIButton ! 
40-   @IBOutlet   private  weak var  videoView :  UIView ! 
4145
42-   /// The entry point of the IMA SDK to make stream requests to Google Ad Manager.
43-   private  var  adsLoader :  IMAAdsLoader ! 
46+   /// The view to display the ad UI elements: countdown, skip button, etc.
47+   /// It is hidden when the stream starts playing.
48+   @IBOutlet   private  weak var  adUIView :  UIView ! 
4449
45-   /// The reference of your video  view for the IMA SDK to create the ad's user interface elements.
50+   /// The reference of your ad UI  view for the IMA SDK to create the ad's user interface elements.
4651  private  var  adDisplayContainer :  IMAAdDisplayContainer ! 
4752
53+   /// The AVPlayer instance that plays the content and the ads.
54+   private  var  player :  AVPlayer ! 
55+ 
4856  /// The reference of your video player for the IMA SDK to play and monitor the ad breaks.
4957  private  var  videoDisplay :  IMAAVPlayerVideoDisplay ! 
5058
59+   /// The entry point of the IMA SDK to make stream requests to Google Ad Manager.
60+   private  var  adsLoader :  IMAAdsLoader ! 
61+ 
5162  /// The reference of the ad stream manager, set when the ad stream is loaded.
5263  /// The IMA SDK requires a strong reference to the stream manager for the entire duration of
5364  /// the ad break.
@@ -60,15 +71,13 @@ class ViewController:
6071
6172    // Initialize the IMA SDK.
6273    let  adLoaderSettings  =  IMASettings ( ) 
63-     // Uncomment the next line for ad UI localization.
64-     // adLoaderSettings.language = "es"
6574    adsLoader =  IMAAdsLoader ( settings:  adLoaderSettings) 
6675
6776    // Set up the video player and the container view.
6877    player =  AVPlayer ( ) 
6978    let  playerLayer  =  AVPlayerLayer ( player:  player) 
70-     playerLayer. frame =  videoView . bounds
71-     videoView . layer. addSublayer ( playerLayer) 
79+     playerLayer. frame =  adUIView . bounds
80+     adUIView . layer. addSublayer ( playerLayer) 
7281    playButton. layer. zPosition =  CGFloat . greatestFiniteMagnitude
7382
7483    // Create an object to monitor the stream playback.
@@ -79,7 +88,7 @@ class ViewController:
7988    // Create a container object for ad UI elements.
8089    // See [example in video ads](https://support.google.com/admanager/answer/2695279#zippy=%2Cexample-in-video-ads)
8190    adDisplayContainer =  IMAAdDisplayContainer ( 
82-       adContainer:  videoView ,  viewController:  self ,  companionSlots:  nil ) 
91+       adContainer:  adUIView ,  viewController:  self ,  companionSlots:  nil ) 
8392
8493    // Specify the delegate for hanlding ad events of the stream session.
8594    adsLoader. delegate =  self 
0 commit comments