เริ่มต้นใช้งาน IMA DAI SDK

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 – ออบเจ็กต์ที่ขอสตรีมและจัดการเหตุการณ์ที่ทริกเกอร์โดยออบเจ็กต์การตอบกลับคำขอสตรีม คุณควรสร้างอินสแตนซ์ของโปรแกรมโหลดโฆษณาเพียงรายการเดียว ซึ่งนํามาใช้ซ้ำได้ตลอดอายุการใช้งานของแอปพลิเคชัน
  • IMAStreamRequest IMAPodVODStreamRequest หรือ IMAPodStreamRequest
  • IMAStreamManager – ออบเจ็กต์ที่จัดการสตรีมการแทรกโฆษณาแบบไดนามิกและการโต้ตอบกับแบ็กเอนด์ DAI นอกจากนี้ เครื่องมือจัดการสตรีมยังจัดการการติดตามคําสั่ง ping และส่งต่อเหตุการณ์สตรีมและโฆษณาไปยังผู้เผยแพร่โฆษณาด้วย

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

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

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

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

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

สร้างโปรเจ็กต์ 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'
    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 โดยไปที่ไฟล์ > เพิ่มแพ็กเกจ

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

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

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

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

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

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

ใช้วิดีโอเพลเยอร์ใน ViewController หลักโดยใช้โปรแกรมเล่น AV ที่รวมอยู่ในมุมมอง UI IMA SDK ใช้มุมมอง UI เพื่อแสดงองค์ประกอบ ad UI

#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

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

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

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

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

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

@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];
}

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

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

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

- (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:adDisplayContainer
                                                  videoDisplay:self.videoDisplay
                                         pictureInPictureProxy:nil
                                                   userContext:nil];
  } else {
    // VOD request. Replace the network code with your value.
    request = [[IMAPodVODStreamRequest alloc] initWithNetworkCode:@kNetworkCode
                                               adDisplayContainer:adDisplayContainer
                                                     videoDisplay:self.videoDisplay
                                            pictureInPictureProxy:nil
                                                      userContext:nil];
  }
  [self.adsLoader requestStreamWithRequest:request];
}

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

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

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

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

- (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.videoDisplay loadStream:streamUrl withSubtitles:@[]];
    [self.videoDisplay 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];
}

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

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

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

/// 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];
};

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

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

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

- (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];
}

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

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

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