ตั้งค่า IMA SDK สําหรับ DAI

IMA SDK ช่วยให้ผสานรวมโฆษณามัลติมีเดียเข้ากับเว็บไซต์และแอปได้อย่างง่ายดาย IMA SDK สามารถ ขอโฆษณาจากเซิร์ฟเวอร์โฆษณา ที่รองรับ VAST และจัดการการเล่นโฆษณาในแอปได้ เมื่อใช้ IMA DAI SDK แอปจะส่ง คำขอสตรีมสำหรับโฆษณาและเนื้อหาวิดีโอ ไม่ว่าจะเป็นเนื้อหา VOD หรือเนื้อหาสด จากนั้น SDK จะแสดงสตรีมวิดีโอที่รวมกัน เพื่อให้คุณไม่ต้องจัดการการสลับระหว่างโฆษณาและวิดีโอเนื้อหา ภายในแอป

เลือกโซลูชัน DAI ที่คุณสนใจ

DAI Pod Serving

คู่มือนี้แสดงวิธีผสานรวม IMA DAI SDK เข้ากับแอปวิดีโอเพลเยอร์อย่างง่าย หากต้องการดูหรือทำตามตัวอย่างการผสานรวมที่เสร็จสมบูรณ์แล้ว ให้ดาวน์โหลด PodServingExample จาก GitHub

ภาพรวม IMA DAI

การติดตั้งใช้งาน IMA DAI มีองค์ประกอบ SDK หลัก 4 อย่างดังที่แสดงใน คู่มือนี้

  • IMAAdDisplayContainer – ออบเจ็กต์คอนเทนเนอร์ที่อยู่ด้านบนขององค์ประกอบการเล่นวิดีโอและมีองค์ประกอบ UI ของโฆษณา
  • IMAAdsLoader – ออบเจ็กต์ที่ขอสตรีมและจัดการเหตุการณ์ที่ทริกเกอร์โดยออบเจ็กต์การตอบกลับคำขอสตรีม คุณควรสร้างอินสแตนซ์ของ AdsLoader เพียงรายการเดียว ซึ่งสามารถนำกลับมาใช้ใหม่ได้ตลอดอายุการใช้งานของแอปพลิเคชัน
  • IMAStreamRequest IMAPodVODStreamRequest หรือ IMAPodStreamRequest
  • IMAStreamManager – ออบเจ็กต์ที่จัดการสตรีมการแทรกโฆษณาแบบไดนามิกและการโต้ตอบกับแบ็กเอนด์ DAI นอกจากนี้ ตัวจัดการสตรีมยังจัดการการติดตาม Ping และส่งต่อเหตุการณ์สตรีมและโฆษณาไปยัง ผู้เผยแพร่โฆษณาด้วย

นอกจากนี้ คุณต้องติดตั้งใช้งานตัวแฮนเดิล VTP ที่กำหนดเองเพื่อเล่นสตรีมการแสดงโฆษณาพ็อด ตัวแฮนเดิล VTP ที่กำหนดเองนี้จะส่งรหัสสตรีม ไปยังพาร์ทเนอร์ด้านเทคนิคของวิดีโอ (VTP) พร้อมกับข้อมูลอื่นๆ ที่ จำเป็นต่อการแสดงผลไฟล์ Manifest ของสตรีมที่มีทั้งเนื้อหาและโฆษณาที่เย็บ VTP จะให้วิธีการเกี่ยวกับวิธีใช้ตัวแฮนเดิล VTP ที่กำหนดเอง

ข้อกำหนดเบื้องต้น

ก่อนเริ่มต้น คุณต้องมีสิ่งต่อไปนี้

นอกจากนี้ คุณยังต้องมีพารามิเตอร์ที่ใช้เพื่อขอสตรีมจาก IMA SDK ด้วย

พารามิเตอร์ไลฟ์สด
รหัสเครือข่าย รหัสเครือข่ายสำหรับบัญชี Ad Manager 360
คีย์เนื้อหาที่กำหนดเอง คีย์ของชิ้นงานที่กำหนดเองซึ่งระบุเหตุการณ์การแสดงโฆษณาพอดใน Ad Manager 360 โดยผู้จัดการไฟล์ Manifest หรือพาร์ทเนอร์ Pod Serving บุคคลที่สามจะเป็นผู้สร้าง
พารามิเตอร์สตรีม VOD
รหัสเครือข่าย รหัสเครือข่ายสำหรับบัญชี Ad Manager 360

สร้างโปรเจ็กต์ Xcode ใหม่

ใน Xcode ให้สร้างโปรเจ็กต์ iOS ใหม่โดยใช้ Objective-C ชื่อ "PodServingExample"

เพิ่ม IMA DAI SDK ลงในโปรเจ็กต์ Xcode

ใช้วิธีใดวิธีหนึ่งต่อไปนี้เพื่อติดตั้ง IMA DAI SDK

ติดตั้ง SDK โดยใช้ CocoaPods (แนะนำ)

CocoaPods เป็นโปรแกรมจัดการทรัพยากร Dependency สำหรับโปรเจ็กต์ Xcode และเป็นวิธีที่แนะนำในการติดตั้ง IMA DAI SDK ดูข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งหรือการใช้ CocoaPods ได้ในเอกสารประกอบสำหรับ CocoaPods หลังจากติดตั้ง CocoaPods แล้ว ให้ใช้วิธีการต่อไปนี้เพื่อติดตั้ง IMA DAI SDK

  1. ในไดเรกทอรีเดียวกับไฟล์ PodServingExample.xcodeproj ให้สร้างไฟล์ข้อความ ชื่อ Podfile แล้วเพิ่มการกำหนดค่าต่อไปนี้

    source 'https://github.com/CocoaPods/Specs.git'
    
    platform :ios, '14'
    
    target 'PodServingExample' do
      pod 'GoogleAds-IMA-iOS-SDK', '~> 3.26.1'
    end
    

  2. จากไดเรกทอรีที่มี Podfile ให้เรียกใช้คำสั่งต่อไปนี้

    pod install --repo-update

ติดตั้ง SDK โดยใช้ Swift Package Manager

Interactive Media Ads SDK รองรับ Swift Package Manager ตั้งแต่เวอร์ชัน 3.18.4 เป็นต้นไป ทำตาม ขั้นตอนต่อไปนี้เพื่อนำเข้าแพ็กเกจ Swift

  1. ใน Xcode ให้ติดตั้งแพ็กเกจ Swift ของ IMA DAI SDK โดยไปที่ File > Add Packages

  2. ในพรอมต์ที่ปรากฏขึ้น ให้ค้นหาที่เก็บ IMA DAI SDK Swift Package GitHub

    https://github.com/googleads/swift-package-manager-google-interactive-media-ads-ios
    
  3. เลือกเวอร์ชันของแพ็กเกจ Swift ของ IMA DAI SDK ที่ต้องการใช้ สำหรับโปรเจ็กต์ใหม่ เราขอแนะนำให้ใช้อัปเดตเป็นเวอร์ชันหลักถัดไป

เมื่อดำเนินการเสร็จแล้ว Xcode จะแก้ไขการขึ้นต่อกันของแพ็กเกจและดาวน์โหลดไว้ในเบื้องหลัง ดูรายละเอียดเพิ่มเติมเกี่ยวกับวิธีเพิ่มการอ้างอิงแพ็กเกจได้ที่บทความของ Apple

ดาวน์โหลดและติดตั้ง SDK ด้วยตนเอง

หากไม่ต้องการใช้ Swift Package Manager หรือ CocoaPods คุณสามารถดาวน์โหลด IMA DAI SDK และเพิ่มลงในโปรเจ็กต์ด้วยตนเองได้

สร้างวิดีโอเพลเยอร์แบบง่าย

ใช้โปรแกรมเล่นวิดีโอในตัวควบคุมมุมมองหลักโดยใช้ AV player ที่ห่อหุ้มในมุมมอง UI IMA SDK ใช้มุมมอง UI เพื่อแสดงองค์ประกอบ UI ของโฆษณา

Objective-C

#import "ViewController.h"

#import <AVKit/AVKit.h>

/// Content URL.
static NSString *const kBackupContentUrl =
    @"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";

@interface ViewController ()
/// Play button.
@property(nonatomic, weak) IBOutlet UIButton *playButton;

@property(nonatomic, weak) IBOutlet UIView *videoView;
/// Video player.
@property(nonatomic, strong) AVPlayer *videoPlayer;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.view.backgroundColor = [UIColor blackColor];

  // Load AVPlayer with the path to your content.
  NSURL *contentURL = [NSURL URLWithString:kBackupContentUrl];
  self.videoPlayer = [AVPlayer playerWithURL:contentURL];

  // Create a player layer for the player.
  AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];

  // Size, position, and display the AVPlayer.
  playerLayer.frame = self.videoView.layer.bounds;
  [self.videoView.layer addSublayer:playerLayer];
}

- (IBAction)onPlayButtonTouch:(id)sender {
  [self.videoPlayer play];
  self.playButton.hidden = YES;
}

@end

Swift

// Copyright 2024 Google LLC. All rights reserved.
//
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, either express or implied. See the License for the specific language governing
// permissions and limitations under the License.

import AVFoundation
import UIKit

class ViewController: UIViewController {

  /// Content URL.
  static let backupStreamURLString =
    "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"

  /// Play button.
  @IBOutlet private weak var playButton: UIButton!

  @IBOutlet private weak var videoView: UIView!
  /// Video player.
  private var videoPlayer: AVPlayer?

  override func viewDidLoad() {
    super.viewDidLoad()

    playButton.layer.zPosition = CGFloat(MAXFLOAT)

    // Load AVPlayer with path to our content.
    // note: this unwrap is safe because the URL is a constant string.
    let contentURL = URL(string: ViewController.backupStreamURLString)!
    videoPlayer = AVPlayer(url: contentURL)

    // Create a player layer for the player.
    let playerLayer = AVPlayerLayer(player: videoPlayer)

    // Size, position, and display the AVPlayer.
    playerLayer.frame = videoView.layer.bounds
    videoView.layer.addSublayer(playerLayer)
  }

  @IBAction func onPlayButtonTouch(_ sender: Any) {
    videoPlayer?.play()
    playButton.isHidden = true
  }
}

เริ่มต้นเครื่องมือโหลดโฆษณา

นำเข้า IMA SDK ไปยังตัวควบคุมมุมมองและใช้โปรโตคอล IMAAdsLoaderDelegate และ IMAStreamManagerDelegate เพื่อจัดการเหตุการณ์ของ AdLoader และ StreamManager

เพิ่มพร็อพเพอร์ตี้ส่วนตัวต่อไปนี้เพื่อจัดเก็บคอมโพเนนต์หลักของ IMA SDK

  • IMAAdsLoader - จัดการคำขอสตรีมตลอดอายุการใช้งานของแอป
  • IMAAdDisplayContainer - จัดการการแทรกและจัดการองค์ประกอบอินเทอร์เฟซผู้ใช้ของโฆษณา
  • IMAAVPlayerVideoDisplay - สื่อสารระหว่าง IMA SDK กับมีเดียเพลเยอร์และจัดการข้อมูลเมตาที่กำหนดเวลา
  • IMAStreamManager - จัดการการเล่นสตรีมและทริกเกอร์เหตุการณ์ที่เกี่ยวข้องกับโฆษณา

เริ่มต้นโปรแกรมโหลดโฆษณา คอนเทนเนอร์ที่แสดงโฆษณา และการแสดงวิดีโอหลังจากที่ โหลดมุมมองแล้ว

Objective-C

@import GoogleInteractiveMediaAds;

// ...

@interface ViewController () <IMAAdsLoaderDelegate, IMAStreamManagerDelegate>
/// The entry point for the IMA DAI SDK to make DAI stream requests.
@property(nonatomic, strong) IMAAdsLoader *adsLoader;
/// The container where the SDK renders each ad's user interface elements and companion slots.
@property(nonatomic, strong) IMAAdDisplayContainer *adDisplayContainer;
/// The reference of your video player for the IMA DAI SDK to monitor playback and handle timed
/// metadata.
@property(nonatomic, strong) IMAAVPlayerVideoDisplay *imaVideoDisplay;
/// References the stream manager from the IMA DAI SDK after successful stream loading.
@property(nonatomic, strong) IMAStreamManager *streamManager;

// ...

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  // ...

  self.adsLoader = [[IMAAdsLoader alloc] initWithSettings:nil];
  self.adsLoader.delegate = self;

  // Create an ad display container for rendering each ad's user interface elements and companion
  // slots.
  self.adDisplayContainer =
      [[IMAAdDisplayContainer alloc] initWithAdContainer:self.videoView
                                          viewController:self
                                          companionSlots:nil];

  // Create an IMAAVPlayerVideoDisplay to give the SDK access to your video player.
  self.imaVideoDisplay = [[IMAAVPlayerVideoDisplay alloc] initWithAVPlayer:self.videoPlayer];
}

Swift

import GoogleInteractiveMediaAds
// ...

class ViewController: UIViewController, IMAAdsLoaderDelegate, IMAStreamManagerDelegate {
  // ...

  /// The entry point for the IMA DAI SDK to make DAI stream requests.
  private var adsLoader: IMAAdsLoader?
  /// The container where the SDK renders each ad's user interface elements and companion slots.
  private var adDisplayContainer: IMAAdDisplayContainer?
  /// The reference of your video player for the IMA DAI SDK to monitor playback and handle timed
  /// metadata.
  private var imaVideoDisplay: IMAAVPlayerVideoDisplay!
  /// References the stream manager from the IMA DAI SDK after successfully loading the DAI stream.
  private var streamManager: IMAStreamManager?

  // ...

  override func viewDidLoad() {
    super.viewDidLoad()

    // ...

    adsLoader = IMAAdsLoader(settings: nil)
    adsLoader?.delegate = self

    // Create an ad display container for rendering each ad's user interface elements and companion
    // slots.
    adDisplayContainer = IMAAdDisplayContainer(
      adContainer: videoView,
      viewController: self,
      companionSlots: nil)

    // Create an IMAAVPlayerVideoDisplay to give the SDK access to your video player.
    imaVideoDisplay = IMAAVPlayerVideoDisplay(avPlayer: videoPlayer)
  }

ส่งคำขอสตรีม

เมื่อผู้ใช้กดปุ่มเล่น ให้ส่งคำขอสตรีมใหม่ ใช้ IMAPodStreamRequest สำหรับไลฟ์สด สำหรับสตรีม VOD ให้ใช้คลาส IMAPodVODStreamRequest

คำขอสตรีมต้องใช้พารามิเตอร์สตรีม รวมถึงการอ้างอิงถึง คอนเทนเนอร์ที่แสดงโฆษณาและวิดีโอที่แสดง

Objective-C

- (IBAction)onPlayButtonTouch:(id)sender {
  [self requestStream];
  self.playButton.hidden = YES;
}

- (void)requestStream {
  // Create a stream request.
  IMAStreamRequest *request;
  if (kStreamType == StreamTypeLive) {
    // Live stream request. Replace the network code and custom asset key with your values.
    request = [[IMAPodStreamRequest alloc] initWithNetworkCode:kNetworkCode
                                                customAssetKey:kCustomAssetKey
                                            adDisplayContainer:self.adDisplayContainer
                                                  videoDisplay:self.imaVideoDisplay
                                         pictureInPictureProxy:nil
                                                   userContext:nil];
  } else {
    // VOD request. Replace the network code with your value.
    request = [[IMAPodVODStreamRequest alloc] initWithNetworkCode:kNetworkCode
                                               adDisplayContainer:self.adDisplayContainer
                                                     videoDisplay:self.imaVideoDisplay
                                            pictureInPictureProxy:nil
                                                      userContext:nil];
  }
  [self.adsLoader requestStreamWithRequest:request];
}

Swift

@IBAction func onPlayButtonTouch(_ sender: Any) {
  requestStream()
  playButton.isHidden = true
}

func requestStream() {
  // Create a stream request. Use one of "Livestream request" or "VOD request".
  if ViewController.requestType == StreamType.live {
    // Livestream request.
    let request = IMAPodStreamRequest(
      networkCode: ViewController.networkCode,
      customAssetKey: ViewController.customAssetKey,
      adDisplayContainer: adDisplayContainer!,
      videoDisplay: self.imaVideoDisplay,
      pictureInPictureProxy: nil,
      userContext: nil)
    adsLoader?.requestStream(with: request)
  } else {
    // VOD stream request.
    let request = IMAPodVODStreamRequest(
      networkCode: ViewController.networkCode,
      adDisplayContainer: adDisplayContainer!,
      videoDisplay: self.imaVideoDisplay,
      pictureInPictureProxy: nil,
      userContext: nil)
    adsLoader?.requestStream(with: request)
  }
}

ฟังเหตุการณ์การโหลดสตรีม

คลาส IMAAdsLoader จะเรียกใช้เมธอด IMAAdsLoaderDelegate เมื่อการเริ่มต้นสำเร็จหรือคำขอสตรีมล้มเหลว

ในเมธอด adsLoadedWithData delegate ให้ตั้งค่า IMAStreamManagerDelegate ส่งรหัสสตรีมไปยังตัวแฮนเดิล VTP ที่กำหนดเอง แล้วดึงข้อมูล URL ของไฟล์ Manifest ของสตรีม สำหรับไลฟ์สด ให้โหลด URL ของไฟล์ Manifest ลงในวิดีโอที่แสดง แล้วเริ่มเล่น สำหรับสตรีม VOD ให้ส่ง URL ของไฟล์ Manifest ไปยังเมธอด loadThirdPartyStream ของตัวจัดการสตรีม วิธีนี้จะขอข้อมูลเหตุการณ์โฆษณาจาก Ad Manager 360 จากนั้นจะโหลด URL ของไฟล์ Manifest และเริ่มการเล่น

ในเมธอด failedWithErrorData ของตัวแทน ให้บันทึกข้อผิดพลาด เล่นสตรีมสำรอง (ไม่บังคับ) ดูแนวทางปฏิบัติแนะนำสำหรับ DAI

Objective-C

- (void)adsLoader:(IMAAdsLoader *)loader adsLoadedWithData:(IMAAdsLoadedData *)adsLoadedData {
  NSLog(@"Stream created with: %@.", adsLoadedData.streamManager.streamId);
  self.streamManager = adsLoadedData.streamManager;
  self.streamManager.delegate = self;

  // Build the Pod serving Stream URL.
  NSString *streamID = adsLoadedData.streamManager.streamId;
  // Your custom VTP handler takes the stream ID and returns the stream manifest URL.
  NSString *urlString = gCustomVTPHandler(streamID);
  NSURL *streamUrl = [NSURL URLWithString:urlString];
  if (kStreamType == StreamTypeLive) {
    // Load live streams directly into the AVPlayer.
    [self.imaVideoDisplay loadStream:streamUrl withSubtitles:@[]];
    [self.imaVideoDisplay play];
  } else {
    // Load VOD streams using the `loadThirdPartyStream` method in IMA SDK's stream manager.
    // The stream manager loads the stream, requests metadata, and starts playback.
    [self.streamManager loadThirdPartyStream:streamUrl streamSubtitles:@[]];
  }
}

- (void)adsLoader:(IMAAdsLoader *)loader failedWithErrorData:(IMAAdLoadingErrorData *)adErrorData {
  // Log the error and play the backup content.
  NSLog(@"AdsLoader error, code:%ld, message: %@", adErrorData.adError.code,
        adErrorData.adError.message);
  [self.videoPlayer play];
}

Swift

func adsLoader(_ loader: IMAAdsLoader, adsLoadedWith adsLoadedData: IMAAdsLoadedData) {
  print("DAI stream loaded. Stream session ID: \(adsLoadedData.streamManager!.streamId!)")
  streamManager = adsLoadedData.streamManager!
  streamManager!.delegate = self

  // Initialize the stream manager to handle ad click and user interactions with ad UI elements.
  streamManager!.initialize(with: nil)

  // Build the Pod serving Stream URL.
  let streamID = streamManager!.streamId
  // Your custom VTP handler takes the stream ID and returns the stream manifest URL.
  let urlString = ViewController.customVTPParser(streamID!)
  let streamUrl = URL(string: urlString)
  if ViewController.requestType == StreamType.live {
    // Live streams can be loaded directly into the AVPlayer.
    imaVideoDisplay.loadStream(streamUrl!, withSubtitles: [])
    imaVideoDisplay.play()
  } else {
    // VOD streams are loaded using the IMA SDK's stream manager.
    // The stream manager loads the stream, requests metadata, and starts playback.
    streamManager!.loadThirdPartyStream(streamUrl!, streamSubtitles: [])
  }
}

func adsLoader(_ loader: IMAAdsLoader, failedWith adErrorData: IMAAdLoadingErrorData) {
  print("Error loading DAI stream. Error message: \(adErrorData.adError.message!)")
  // Play the backup stream.
  videoPlayer.play()
}

ใช้ตัวแฮนเดิล VTP ที่กำหนดเอง

แฮนเดิล VTP ที่กำหนดเองจะส่งรหัสสตรีมของผู้ชมไปยังพาร์ทเนอร์ด้านเทคนิคของวิดีโอ (VTP) พร้อมกับข้อมูลอื่นๆ ที่ VTP ต้องการเพื่อแสดงผล ไฟล์ Manifest ของสตรีมที่มีทั้งเนื้อหาและโฆษณาที่ต่อกัน VTP จะ ให้วิธีการที่เฉพาะเจาะจงเกี่ยวกับวิธีติดตั้งใช้งานตัวแฮนเดิล VTP ที่กำหนดเอง

เช่น VTP อาจมี URL เทมเพลตไฟล์ Manifest ที่มีมาโคร [[STREAMID]] ในตัวอย่างนี้ ตัวแฮนเดิลจะแทรกรหัสสตรีมแทนที่มาโครและแสดงผล URL ของไฟล์ Manifest ที่ได้

Objective-C

/// Custom VTP Handler.
///
/// Returns the stream manifest URL from the video technical partner or manifest manipulator.
static NSString *(^gCustomVTPHandler)(NSString *) = ^(NSString *streamID) {
  // Insert synchronous code here to retrieve a stream manifest URL from your video tech partner
  // or manifest manipulation server.
  // This example uses a hardcoded URL template, containing a placeholder for the stream
  // ID and replaces the placeholder with the stream ID.
  NSString *manifestUrl = @"YOUR_MANIFEST_URL_TEMPLATE";
  return [manifestUrl stringByReplacingOccurrencesOfString:@"[[STREAMID]]"
                                                withString:streamID];
};

Swift

/// Custom VTP Handler.
///
/// Returns the stream manifest URL from the video technical partner or manifest manipulator.
static let customVTPParser = { (streamID: String) -> (String) in
  // Insert synchronous code here to retrieve a stream manifest URL from your video tech partner
  // or manifest manipulation server.
  // This example uses a hardcoded URL template, containing a placeholder for the stream
  // ID and replaces the placeholder with the stream ID.
  let manifestURL = "YOUR_MANIFEST_URL_TEMPLATE"
  return manifestURL.replacingOccurrences(of: "[[STREAMID]]", with: streamID)
}

ฟังเหตุการณ์โฆษณา

IMAStreamManager เรียกใช้เมธอด IMAStreamManagerDelegate เพื่อส่งเหตุการณ์และข้อผิดพลาดของสตรีมไปยังแอปพลิเคชัน

ในตัวอย่างนี้ ให้บันทึกเหตุการณ์โฆษณาหลักไปยังคอนโซล

Objective-C

- (void)streamManager:(IMAStreamManager *)streamManager didReceiveAdEvent:(IMAAdEvent *)event {
  NSLog(@"Ad event (%@).", event.typeString);
  switch (event.type) {
    case kIMAAdEvent_STARTED: {
      // Log extended data.
      NSString *extendedAdPodInfo = [[NSString alloc]
          initWithFormat:@"Showing ad %ld/%ld, bumper: %@, title: %@, description: %@, contentType:"
                         @"%@, pod index: %ld, time offset: %lf, max duration: %lf.",
                         (long)event.ad.adPodInfo.adPosition, (long)event.ad.adPodInfo.totalAds,
                         event.ad.adPodInfo.isBumper ? @"YES" : @"NO", event.ad.adTitle,
                         event.ad.adDescription, event.ad.contentType,
                         (long)event.ad.adPodInfo.podIndex, event.ad.adPodInfo.timeOffset,
                         event.ad.adPodInfo.maxDuration];

      NSLog(@"%@", extendedAdPodInfo);
      break;
    }
    case kIMAAdEvent_AD_BREAK_STARTED: {
      NSLog(@"Ad break started");
      break;
    }
    case kIMAAdEvent_AD_BREAK_ENDED: {
      NSLog(@"Ad break ended");
      break;
    }
    case kIMAAdEvent_AD_PERIOD_STARTED: {
      NSLog(@"Ad period started");
      break;
    }
    case kIMAAdEvent_AD_PERIOD_ENDED: {
      NSLog(@"Ad period ended");
      break;
    }
    default:
      break;
  }
}

- (void)streamManager:(IMAStreamManager *)streamManager didReceiveAdError:(IMAAdError *)error {
  NSLog(@"StreamManager error with type: %ld\ncode: %ld\nmessage: %@", error.type, error.code,
        error.message);
  [self.videoPlayer play];
}

Swift

func streamManager(_ streamManager: IMAStreamManager, didReceive event: IMAAdEvent) {
  print("Ad event \(event.typeString).")
  switch event.type {
  case IMAAdEventType.STARTED:
    // Log extended data.
    if let ad = event.ad {
      let extendedAdPodInfo = String(
        format: "Showing ad %zd/%zd, bumper: %@, title: %@, "
          + "description: %@, contentType:%@, pod index: %zd, "
          + "time offset: %lf, max duration: %lf.",
        ad.adPodInfo.adPosition,
        ad.adPodInfo.totalAds,
        ad.adPodInfo.isBumper ? "YES" : "NO",
        ad.adTitle,
        ad.adDescription,
        ad.contentType,
        ad.adPodInfo.podIndex,
        ad.adPodInfo.timeOffset,
        ad.adPodInfo.maxDuration)

      print("\(extendedAdPodInfo)")
    }
    break
  case IMAAdEventType.AD_BREAK_STARTED:
    print("Ad break started.")
    break
  case IMAAdEventType.AD_BREAK_ENDED:
    print("Ad break ended.")
    break
  case IMAAdEventType.AD_PERIOD_STARTED:
    print("Ad period started.")
    break
  case IMAAdEventType.AD_PERIOD_ENDED:
    print("Ad period ended.")
    break
  default:
    break
  }
}

func streamManager(_ streamManager: IMAStreamManager, didReceive error: IMAAdError) {
  print("StreamManager error with type: \(error.type)")
  print("code: \(error.code)")
  print("message: \(error.message ?? "Unknown Error")")
}

ล้างข้อมูลชิ้นงาน IMA DAI

หากต้องการหยุดการเล่นสตรีม หยุดการติดตามโฆษณาทั้งหมด และปล่อยชิ้นงานสตรีมทั้งหมดที่โหลดไว้ ให้เรียกใช้ IMAStreamManager.destroy()

เรียกใช้แอป และหากสำเร็จ คุณจะขอและเล่นสตรีม Google DAI ด้วย IMA SDK ได้ ดูข้อมูลเกี่ยวกับฟีเจอร์ SDK ขั้นสูงเพิ่มเติมได้ในคำแนะนำอื่นๆ ที่แสดงในแถบด้านข้างทางซ้ายหรือตัวอย่างใน GitHub