在 iOS 上使用 IMA DAI SDK

播放使用 Google Cloud Video Stitcher API 註冊的 VOD 串流

本指南將說明如何使用 iOS 版 IMA DAI SDK 來要求及播放 Google Cloud VOD 串流工作階段

本指南將 IMA DAI 的入門指南中的基本範例做進一步說明。

如要瞭解如何整合其他平台,或使用 IMA 用戶端 SDK,請參閱「互動式媒體廣告 SDK」。

如要查看或瞭解完成的整合範例,請下載 Objective-CSwift 的 Cloud 影片拼接器範例。

設定 Google Cloud 專案

輸入下列變數,以便在 IMA SDK 中使用:

位置
建立 VOD 設定的 Google Cloud 區域LOCATION
專案編號
使用 Video Stitcher API 的 Google Cloud 專案編號: PROJECT_NUMBER
OAuth 權杖

服務帳戶的短期 OAuth 權杖,具有 Video Stitcher 使用者角色:

OAUTH_TOKEN

進一步瞭解如何建立短效 OAuth 權杖。只要 OAuth 權杖未過期,就可以在多個要求中重複使用。

聯播網代碼

用於要求廣告的 Ad Manager 聯播網代碼: NETWORK_CODE

VOD 設定 ID

VOD 串流的 VOD 設定 ID: VOD_CONFIG_ID

如要進一步瞭解如何建立 VOD 設定 ID,請參閱「雲端拼接功能建立 VOD 設定指南」。

使用者情境
追蹤要求的使用者背景資訊。可以是 nil本指南的預設值為 nil

設定基本範例

前往 IMA iOS DAI GitHub 發布頁面,下載基本的 Objective-C 範例。這個範例是 iOS Xcode 專案,會依賴 Cocoapods 載入 IMA DAI SDK。如果您使用 Swift,IMA 並未提供 iOS 範例應用程式,但請參閱本指南稍後的 Swift 程式碼片段,瞭解如何在自己的應用程式中導入 IMAVideoStitcherVODStreamRequest

如要準備要執行的範例,請確認已安裝 CocoaPods,然後在終端機中開啟基本範例的資料夾,並執行下列指令:

pod install --repo-update

指令完成後,專案資料夾中會出現名為 BasicExample.xcworkspace 的檔案。在 Xcode 中開啟這個檔案並執行範例,確認測試影片和廣告能正常播放。

要求 VOD 串流

如要將樣本串流取代為拼接廣告的 VOD 串流,請使用 IMAVideoStitcherVODStreamRequest 透過 Google Ad Manager 建立廣告工作階段。您可以使用 Google Ad Manager UI 找出產生的動態廣告插播廣告工作階段,以便監控及偵錯。

在現有範例中,有向 Google DAI 伺服器要求 VOD 串流或直播的範例。如要讓其與 Google Cloud Video Stitcher API 搭配運作,您必須將目前的 requestStream 函式替換為使用 IMAVideoStitcherVODStreamRequest 類別的函式。

範例如下:

Objective-C

ViewController.m

...

#import <GoogleInteractiveMediaAds/GoogleInteractiveMediaAds.h>
/// The Stream's VOD config ID.
static NSString *const kVODConfigID = @"VOD_CONFIG_ID";
/// The Network Code used by the Video Stitcher API.
static NSString *const kNetworkCode = @"NETWORK_CODE";
/// The Google Cloud project using the Video Stitcher API.
static NSString *const kProjectNumber = @"PROJECT_NUMBER";
/// The Google Cloud region containing your project.
static NSString *const kLocation = @"LOCATION";
/// An OAuth Token created by a Google Cloud account with Video Stitcher API
/// permissions.
static NSString *const kOAuthToken = @"OAUTH_TOKEN";

/// Fallback URL in case something goes wrong in loading the stream. If all goes well, this will not
/// be used.
static NSString *const kBackupStreamURLString =
    @"http://googleimadev-vh.akamaihd.net/i/big_buck_bunny/bbb-,480p,720p,1080p,.mov.csmil/"
    @"master.m3u8";
@interface ViewController () <IMAAdsLoaderDelegate, IMAStreamManagerDelegate>

...

- (void)requestStream {
    // Create an ad display container for ad rendering.
    IMAAdDisplayContainer *adDisplayContainer =
        [[IMAAdDisplayContainer alloc] initWithAdContainer:self.videoView
                                            viewController:self
                                            companionSlots:nil];
    // Create an IMAAVPlayerVideoDisplay to give the SDK access to your video player.
    IMAAVPlayerVideoDisplay *imaVideoDisplay =
        [[IMAAVPlayerVideoDisplay alloc] initWithAVPlayer:self.contentPlayer];
    IMAVideoStitcherVODStreamRequest *streamRequest =
        [[IMAVideoStitcherVODStreamRequest alloc] initWithVODConfigID:kVODConfigID
                                                             region:kLocation
                                                      projectNumber:kProjectNumber
                                                         OAuthToken:kOAuthToken
                                                        networkCode:kNetworkCode
                                                 adDisplayContainer:adDisplayContainer
                                                       videoDisplay:imaVideoDisplay
                                                        userContext:nil
                                        videoStitcherSessionOptions:nil];
    [self.adsLoader requestStreamWithRequest:streamRequest];
}

...

Swift

ViewController.swift

...

class ViewController: UIViewController, IMAAdsLoaderDelegate, IMAStreamManagerDelegate {
  /// The Stream's VOD config ID.
  static let vodConfigID = "VOD_CONFIG_ID"
  /// The Network Code used by the Video Stitcher API.
  static let networkCode = "NETWORK_CODE"
  /// The Google Cloud project using the Video Stitcher API.
  static let projectNumber = "PROJECT_NUMBER"
  /// The Google Cloud region containing your project.
  static let location = "LOCATION"
  /// An OAuth Token created by a Google Cloud account with Video Stitcher API
  /// permissions.
  static let oAuthToken = "OAUTH_TOKEN"

  /// Fallback URL in case something goes wrong in loading the stream. If all goes well, this will
  /// not be used.
  static let backupStreamURLString = """
    http://googleimadev-vh.akamaihd.net/i/big_buck_bunny/\
    bbb-,480p,720p,1080p,.mov.csmil/master.m3u8
    """

  ...

  func requestStream() {
    // Create an ad display container for ad rendering.
    adDisplayContainer = IMAAdDisplayContainer(
      adContainer: videoView,
      viewController: self,
      companionSlots: nil)
    // Create an IMAAVPlayerVideoDisplay to give the SDK access to your video player.
    let imaVideoDisplay = IMAAVPlayerVideoDisplay(avPlayer: contentPlayer!)
    // Create a VOD stream request.
    let streamRequest = IMAVideoStitcherVODStreamRequest(
      VODConfigID: ViewController.vodConfigID,
      region: ViewController.location,
      projectNumber: ViewController.projectNumber,
      oAuthToken: ViewController.oAuthToken,
      networkCode: ViewController.networkCode,
      adDisplayContainer: adDisplayContainer!,
      videoDisplay: imaVideoDisplay,
      userContext: nil,
      videoStitcherSessionOptions: nil)
    adsLoader?.requestStream(with: streamRequest)
  }

  ...

執行專案後,您就可以要求並播放自訂 VOD 串流。

(選用) 新增串流工作階段選項

如要自訂串流要求,請在 IMAVideoStitcherVODStreamRequest 中填入 videoStitcherSessionOptions 參數,藉此新增工作階段選項來覆寫預設的 Cloud Video Stitcher API 設定。如果您提供未知的選項,Cloud Video Stitcher API 會回應 HTTP 400 錯誤。如需協助,請參閱疑難排解指南

舉例來說,您可以使用以下程式碼片段覆寫資訊清單選項,要求兩個串流資訊清單,並以最低比特率至最高比特率的順序排列呈現內容。

Objective-C

 // Define session options JSON string.
 // The following session options are examples. Use session options
 // that are compatible with your video stream.
 NSString *sessionOptionsStr =
   @"{"
   "  \"manifestOptions\": {"
   "    \"bitrateOrder\": \"ascending\""
   "  }"
   "}";
 // convert JSON NSString to NSDictionary
 NSData *sessionOptionsData = [sessionOptionsStr dataUsingEncoding:NSUTF8StringEncoding];
 NSError *error = nil;
 NSDictionary *sessionOptions = [NSJSONSerialization
                       JSONObjectWithData:sessionOptionsData
                       options:0
                       error:&error];
 // make stream request
 IMAVideoStitcherVODStreamRequest *streamRequest =
     [[IMAVideoStitcherVODStreamRequest alloc] initWithVODConfigID:kVODConfigID
                                                           region:kLocation
                                                     projectNumber:kProjectNumber
                                                       OAuthToken:kOAuthToken
                                                       networkCode:kNetworkCode
                                               adDisplayContainer:adDisplayContainer
                                                     videoDisplay:imaVideoDisplay
                                                       userContext:nil
                                       videoStitcherSessionOptions:sessionOptions];
 [self.adsLoader requestStreamWithRequest:streamRequest];

Swift

 // Define session options JSON string.
 // The following session options are examples. Use session options
 // that are compatible with your video stream.
 let sessionOptionsStr = """
     {
       "manifestOptions": {
         "bitrateOrder": "ascending"
       }
     }
     """
 // convert JSON string to dictionary
 guard let sessionOptionsData = sessionOptionsStr.data(using: .utf8, allowLossyConversion: false) else { return nil }
 let sessionOptions = try? JSONSerialization.jsonObject(with: sessionOptionsData, options: .mutableContainers)
 // make stream request
 let streamRequest = IMAVideoStitcherVODStreamRequest(
   vodConfigID:ViewController.vodConfigID
   region:ViewController.location
   projectNumber:ViewController.projectNumber
   OAuthToken:ViewController.oAuthToken
   networkCode:ViewController.networkCode
   adDisplayContainer:adDisplayContainer
   videoDisplay:imaVideoDisplay
   userContext:nil
   videoStitcherSessionOptions:sessionOptions)
 adsLoader?.requestStream(with: streamRequest)

清除所用資源

您已成功使用 Google Cloud Video Stitcher API 代管 VOD 串流,並使用 iOS 版 IMA DAI SDK 進行要求,因此請務必清除所有放送資源。

請按照VOD 清理指南移除所有不必要的資源和素材資源。