播放透過 Google Cloud Video Stitcher API 註冊的 VOD 串流
本指南說明如何使用 IMA DAI SDK for iOS,請求及播放 Google Cloud 隨選影片串流工作階段。
本指南將進一步說明 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 權杖
具備 Video Stitcher 使用者角色的服務帳戶短期 OAuth 權杖:
OAUTH_TOKEN
進一步瞭解如何建立短期 OAuth 權杖。只要 OAuth 權杖未過期,就能在多個要求中重複使用。
- 聯播網代碼
請求廣告的 Ad Manager 聯播網代碼:
NETWORK_CODE
- VOD 設定 ID
VOD 串流的 VOD 設定 ID:
VOD_CONFIG_ID如要進一步瞭解如何建立 VOD 設定 ID,請參閱「Cloud Stitching 建立 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 串流,並透過 IMA DAI SDK for iOS 提出要求,現在請務必清除所有放送資源。
請按照隨選影片清理指南,移除所有不必要的資源和資產。