ตั้งค่าด้วย DAI แบบบริการเต็มรูปแบบ

เลือกแพลตฟอร์ม: HTML5 Android iOS tvOS Cast Roku

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

ภาพรวม IMA DAI

การติดตั้งใช้งาน IMA DAI เกี่ยวข้องกับคอมโพเนนต์ SDK หลัก 4 รายการ ดังที่คู่มือนี้แสดงให้เห็น

  • IMAAdDisplayContainer – ออบเจ็กต์คอนเทนเนอร์ที่อยู่ด้านบนขององค์ประกอบการเล่นวิดีโอและเป็นที่เก็บองค์ประกอบ UI ของโฆษณา
  • IMAAdsLoader – ออบเจ็กต์ที่ขอสตรีมและจัดการเหตุการณ์ที่ทริกเกอร์โดยออบเจ็กต์การตอบกลับคำขอสตรีม คุณควรสร้างอินสแตนซ์ตัวโหลดโฆษณาเพียงรายการเดียว ซึ่งสามารถนำมาใช้ซ้ำได้ตลอดอายุการใช้งานของแอปพลิเคชัน
  • IMAStreamRequest – IMAVODStreamRequest หรือ IMALiveStreamRequest ออบเจ็กต์ที่กำหนดคำขอสตรีม คำขอสตรีมอาจเป็นวิดีโอออนดีมานด์หรือไลฟ์สดก็ได้ คำขอไลฟ์สดจะระบุคีย์เนื้อหา ส่วนคำขอ VOD จะระบุรหัส CMS และรหัสวิดีโอ คำขอทั้ง 2 ประเภทสามารถเลือกใส่คีย์ API ที่จำเป็นในการเข้าถึงสตรีมที่ระบุ และรหัสเครือข่าย Ad Manager ของ Google เพื่อให้ IMA SDK จัดการตัวระบุโฆษณาตามที่ระบุไว้ในการตั้งค่า Ad Manager ของ Google
  • IMAStreamManager – ออบเจ็กต์ที่จัดการสตรีมการแทรกโฆษณาแบบไดนามิกและการโต้ตอบกับแบ็กเอนด์ DAI นอกจากนี้ ตัวจัดการสตรีมยังจัดการคำสั่ง ping การติดตามและส่งต่อเหตุการณ์สตรีมและโฆษณาไปยังผู้เผยแพร่โฆษณาด้วย

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

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

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

พารามิเตอร์ไลฟ์สด
คีย์เนื้อหา คีย์เนื้อหาที่ระบุไลฟ์สดของคุณใน Google Ad Manager
ตัวอย่าง: c-rArva4ShKVIAkNfy6HUQ
พารามิเตอร์สตรีม VOD
รหัสแหล่งที่มาของเนื้อหา รหัสแหล่งที่มาของเนื้อหาจาก Google Ad Manager
ตัวอย่าง: 2548831
รหัสวิดีโอ รหัสวิดีโอจาก Google Ad Manager
ตัวอย่าง: tears-of-steel
พารามิเตอร์ทั่วไป (VOD และไลฟ์สด)
รหัสเครือข่าย รหัสเครือข่าย Ad Manager ของ Google
ตัวอย่าง: 21775744923

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

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

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

หากต้องการติดตั้ง IMA SDK ให้เลือกวิธีการที่ต้องการ

แนะนำ: ติดตั้ง SDK โดยใช้ Swift Package Manager

SDK โฆษณาสื่ออินเทอร์แอกทีฟรองรับ Swift Package Manager ตั้งแต่เวอร์ชัน 3.18.4 เป็นต้นไป ทำตามขั้นตอนเหล่านี้เพื่อนำเข้าแพ็กเกจ Swift

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

  2. ในข้อความแจ้งที่ปรากฏขึ้น ให้ค้นหาที่เก็บ GitHub ของแพ็กเกจ Swift สำหรับ IMA DAI SDK ดังนี้

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

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

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

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

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

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

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 ไปยัง View Controller และใช้โปรโตคอล IMAAdsLoaderDelegate และ IMAStreamManagerDelegate เพื่อจัดการเหตุการณ์ของตัวโหลดโฆษณาและตัวจัดการสตรีม

เพิ่มพร็อพเพอร์ตี้ส่วนตัวเหล่านี้เพื่อจัดเก็บคอมโพเนนต์หลักของ 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 ad UI elements and the companion ad.
    adDisplayContainer = IMAAdDisplayContainer(
      adContainer: videoView,
      viewController: self,
      companionSlots: nil)

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

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

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

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

Objective-C

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

- (void)requestStream {
  // Create a stream request. Use one of "Live stream request" or "VOD request", depending on your
  // type of stream.
  if (kStreamType == StreamTypeLive) {
    // Live stream request. Replace the asset key with your value.
    IMALiveStreamRequest *request =
        [[IMALiveStreamRequest alloc] initWithAssetKey:kLiveStreamAssetKey
                                           networkCode:kNetworkCode
                                    adDisplayContainer:self.adDisplayContainer
                                          videoDisplay:self.imaVideoDisplay
                                           userContext:nil];
    request.useHLSInterstitials = YES;
    [self.adsLoader requestStreamWithRequest:request];
  } else {
    // VOD request. Replace the content source ID and video ID with your values.
    IMAVODStreamRequest *request =
        [[IMAVODStreamRequest alloc] initWithContentSourceID:kVODContentSourceID
                                                     videoID:kVODVideoID
                                                 networkCode:kNetworkCode
                                          adDisplayContainer:self.adDisplayContainer
                                                videoDisplay:self.imaVideoDisplay
                                                 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 = IMALiveStreamRequest(
      assetKey: ViewController.assetKey,
      networkCode: ViewController.networkCode,
      adDisplayContainer: adDisplayContainer!,
      videoDisplay: imaVideoDisplay,
      userContext: nil)
    request.useHLSInterstitials = true
    adsLoader?.requestStream(with: request)
  } else {
    // VOD stream request.
    let request = IMAVODStreamRequest(
      contentSourceID: ViewController.contentSourceID,
      videoID: ViewController.videoID,
      networkCode: ViewController.networkCode,
      adDisplayContainer: adDisplayContainer!,
      videoDisplay: imaVideoDisplay,
      userContext: nil)
    adsLoader?.requestStream(with: request)
  }
}

รอเหตุการณ์การโหลดสตรีม

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

ในเมธอดตัวแทน adsLoadedWithData ให้ตั้งค่า IMAStreamManagerDelegate เริ่มต้นใช้งานเครื่องมือจัดการสตรีม เมื่อเริ่มต้นใช้งาน ตัวจัดการสตรีมจะเริ่มเล่น

ในเมธอดผู้รับมอบสิทธิ์ 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;
  [self.streamManager initializeWithAdsRenderingSettings:nil];
}

- (void)adsLoader:(IMAAdsLoader *)loader failedWithErrorData:(IMAAdLoadingErrorData *)adErrorData {
  // Log the error and play the 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
  streamManager!.initialize(with: nil)
}

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

รอเหตุการณ์โฆษณา

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")")
}

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