播放透過 Google Cloud Video Stitcher API 註冊的 VOD 串流
本指南說明如何使用 Roku 專用 IMA DAI SDK,請求及播放 Google Cloud 隨選影片串流工作階段。
本指南將進一步說明 IMA DAI 入門指南中的基本範例。
如要瞭解如何與其他平台整合,或如何使用 IMA 用戶端 SDK,請參閱「互動式媒體廣告 SDK」。
如要查看或按照完整的整合範例操作,請下載 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 設定指南」。
設定基本範例
下載並執行 IMA Roku DAI 基本範例。點選影片播放器上的播放按鈕,即可開始播放短片《鋼鐵之淚》,其中每 30 秒會插播一次廣告。
要求 VOD 串流
使用 sdk.createVideoStitcherVodStreamRequestWithVodConfig() 函式,將範例串流替換為廣告拼接隨選影片串流。您可以使用 Google Ad Manager UI 找出產生的 DAI 工作階段,以便監控及偵錯。
在現有範例中,有條件陳述式可判斷是否要建構 VOD StreamRequest 或直播 StreamRequest。新增路徑,使用先前產生的值建構 Cloud Video Stitcher VOD StreamRequest。
範例如下:
basic_example/components/MainScene.xml
<?xml version="1.0" encoding="utf-8" ?>
<component name="MainScene" extends="Scene" initialFocus = "myVideo">
<script type="text/brightscript">
<![CDATA[
function init()
m.video = m.top.findNode("myVideo")
m.video.notificationinterval = 1
m.testLiveStream = {
title: "Livestream",
assetKey: "c-rArva4ShKVIAkNfy6HUQ",
apiKey: "",
type: "live"
}
m.testVodStream = {
title: "VOD stream"
contentSourceId: "2548831",
videoId: "tears-of-steel",
apiKey: "",
type: "vod"
}
m.testVideoStitcherVodStream = {
title: "My Stream Title",
vodConfigId: "VOD_CONFIG_ID",
networkCode: "NETWORK_CODE",
region: "LOCATION",
projectNumber: "PROJECT_NUMBER",
oAuthToken: "OAUTH_TOKEN",
apiKey: "",
title: "Video Stitcher VOD Stream",
type: "stitcherVod"
}
loadImaSdk()
end function
function loadImaSdk() as void
m.sdkTask = createObject("roSGNode", "imasdk")
m.sdkTask.observeField("sdkLoaded", "onSdkLoaded")
m.sdkTask.observeField("errors", "onSdkLoadedError")
' Set this to the stream data you would like to play.
selectedStream = m.testVideoStitcherVodStream
m.videoTitle = selectedStream.title
m.sdkTask.streamData = selectedStream
m.sdkTask.observeField("urlData", "urlLoadRequested")
m.sdkTask.video = m.video
' Setting control to run starts the task thread.
m.sdkTask.control = "RUN"
end function
basic_example/components/Sdk.xml
Sub loadStream()
sdk = m.sdk
sdk.initSdk()
setupVideoPlayer()
request = {}
streamData = m.top.streamData
if streamData.type = "live"
request = sdk.CreateLiveStreamRequest(streamData.assetKey, streamData.apiKey)
else if streamData.type = "vod"
request = sdk.CreateVodStreamRequest(streamData.contentSourceId, streamData.videoId, streamData.apiKey)
else if streamData.type = "stitcherVod"
request = sdk.createVideoStitcherVodStreamRequest(
streamData.vodConfigId,
streamData.networkCode,
streamData.region,
streamData.projectNumber,
streamData.oAuthToken
)
else
request = sdk.CreateStreamRequest()
end if
request.player = m.player
request.adUiNode = m.top.video
requestResult = sdk.requestStream(request)
If requestResult <> Invalid
print "Error requesting stream ";requestResult
Else
m.streamManager = Invalid
While m.streamManager = Invalid
sleep(50)
m.streamManager = sdk.getStreamManager()
End While
If m.streamManager = Invalid or m.streamManager["type"] <> Invalid or m.streamManager["type"] = "error"
errors = CreateObject("roArray", 1, True)
print "error ";m.streamManager["info"]
errors.push(m.streamManager["info"])
m.top.errors = errors
Else
m.top.streamManagerReady = True
addCallbacks()
m.streamManager.start()
End If
End If
End Sub
重新載入應用程式,要求並播放自訂 VOD 串流。
(選用) 新增串流工作階段選項
如要自訂串流請求,請新增工作階段選項,並在 StreamRequest 物件中填入 videoStitcherSessionOptions 參數,覆寫預設的 Cloud Video Stitcher API 設定。
如果您提供無法辨識的選項,Cloud Video Stitcher API 會傳回 HTTP 400 錯誤。如需協助,請參閱疑難排解指南。
舉例來說,您可以使用下列程式碼片段覆寫資訊清單選項,要求兩個串流資訊清單,並依位元率由低到高排序。
request = sdk.createVideoStitcherVodStreamRequestWithVodConfig(vodConfigId, networkCode, region, projectNumber, oAuthToken)
request.player = m.player
request.adUiNode = m.top.video
' The following session options are examples. Use session options
' that are compatible with your video stream.
sessionOptions = {
"manifestOptions": {
"bitrateOrder": "ascending"
}
}
request.videoStitcherSessionOptions = sessionOptions
requestResult = sdk.requestStream(request)
清除所用資源
您已成功使用 Google Cloud Video Stitcher API 代管 VOD 串流,並透過 Roku 適用的 IMA DAI SDK 提出請求,現在請務必清除所有放送資源。
請按照隨選影片清理指南,移除所有不必要的資源和資產。