在 iOS 上使用 IMA DAI SDK

播放使用 Google Cloud Video Stitcher API 註冊的直播

本指南將說明如何使用 iOS 版 IMA DAI SDK,針對已註冊 Google Cloud Video Stitcher API 的活動,要求及播放直播,以及如何在播放期間插入廣告插播。

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

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

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

設定 Google Cloud 專案

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

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

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

OAUTH_TOKEN

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

聯播網代碼

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

直播設定 ID
建立直播活動時指定的直播設定 ID: LIVE_CONFIG_ID
自訂素材資源金鑰
使用 Video Stitcher API 建立直播活動設定時,Ad Manager 會產生自訂素材資源鍵: CUSTOM_ASSET_KEY

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

下載並準備基本範例

下載 iOS 適用的 IMA DAI 範例,並將基本範例解壓縮至新資料夾。這個範例是 Xcode 專案,會使用 Cocoapods 載入 IMA SDK。

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

pod install --repo-update

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

要求直播

如要將樣本串流取代為直播,您必須使用 IMAVideoStitcherLiveStreamRequest 類別,讓系統自動透過 Google Ad Manager 建立廣告工作階段。您可以使用 Google Ad Manager UI 找出產生的 DAI 工作階段,用於監控和偵錯。

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

ViewController.m

#import <GoogleInteractiveMediaAds/GoogleInteractiveMediaAds.h>

/// Fallback URL in case something goes wrong in loading the stream. If all goes well,
/// this will not be used.
static NSString *const kTestAppContentUrl_M3U8 =
    @"//devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";


static NSString *const kLiveConfigID = @"LIVE_CONFIG_ID";
static NSString *const kLocation = @"LOCATION";
static NSString *const kProjectNumber = @"PROJECT_NUMBER";
static NSString *const kOAuthToken = @"OAUTH_TOKEN";
static NSString *const kNetworkCode = @"NETWORK_CODE";
static NSString *const kCustomAssetKey = @"CUSTOM_ASSET_KEY";


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


  IMAVideoStitcherLiveStreamRequest *streamRequest =
      [[IMAVideoStitcherLiveStreamRequest alloc] initWithLiveStreamEventID:kLiveConfigID
                                                                    region:kLocation
                                                             projectNumber:kProjectNumber
                                                                OAuthToken:kOAuthToken
                                                               networkCode:kNetworkCode
                                                            customAssetKey:kCustomAssetKey
                                                        adDisplayContainer:adDisplayContainer
                                                              videoDisplay:imaVideoDisplay
                                                               userContext:nil
                                                               videoStitcherSessionOptions:nil];

  [self.adsLoader requestStreamWithRequest:streamRequest];
}

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

如要自訂串流要求,請在 IMAVideoStitcherLiveStreamRequest 中填入 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
 IMAVideoStitcherLiveStreamRequest *streamRequest =
     [[IMAVideoStitcherLiveStreamRequest alloc] initWithLiveStreamEventID:kLiveConfigID
                                                                   region:kLocation
                                                           projectNumber:kProjectNumber
                                                               OAuthToken:kOAuthToken
                                                             networkCode:kNetworkCode
                                                           customAssetKey:kCustomAssetKey
                                                       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 = IMAVideoStitcherLiveStreamRequest(
   liveStreamEventID:ViewController.liveConfigID
   region:ViewController.location
   projectNumber:ViewController.projectNumber
   OAuthToken:ViewController.oAuthToken
   networkCode:ViewController.networkCode
   customAssetKey:ViewController.customAssetKey
   adDisplayContainer:adDisplayContainer
   videoDisplay:imaVideoDisplay
   userContext:nil
   videoStitcherSessionOptions:sessionOptions)
 adsLoader?.requestStream(with: streamRequest)

執行專案後,即可要求並播放自訂直播。

插入廣告插播

Google Cloud Video Stitcher API 會為每個廣告插播插入從廣告代碼擷取的廣告。廣告插播時間點會在資訊清單中以廣告標記表示。廣告標記是由直播編碼器插入。

廣告插播後立即播放廣告。

清除所用資源

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

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