播放透過 Google Cloud Video Stitcher API 註冊的 VOD 串流
本指南說明如何使用 iOS 適用的 IMA DAI SDK,要求及播放 Google Cloud VOD 串流工作階段。
本指南將擴充入門指南中的基本範例,說明如何使用 IMA DAI。
如要瞭解如何與其他平台整合,或使用 IMA 用戶端 SDK,請參閱「互動式媒體廣告 SDK」。
如要查看或跟著完成整合範例,請下載 Objective-C 或 Swift 的 Cloud Video Stitcher 範例。
設定 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
隨選影片串流的隨選影片設定 ID:
VOD_CONFIG_ID如要進一步瞭解如何建立 VOD 設定 ID,請參閱「Cloud 縫合建立 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 使用者介面找出產生的 DAI 工作階段,以便監控及偵錯。
在現有範例中,有從 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 清理指南移除所有不必要的資源和資產。