为 CAF DAI SDK 编写发送方应用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
与之前 Chromecast 和 DAI 之间的集成不同,CAF DAI SDK 非常注重维护 Cast 应用框架设定的标准和预期。因此,您无需对发送方应用进行大量自定义,即可管理 IMA 与发送方应用之间的双向通信。这些通信现在通过集成的 CAF 广告插播功能进行管理,而无需自定义消息系统在发送方应用和接收方之间进行通信。
根据发送方的不同,您可能仍需向发送方添加一些有限的代码,才能完全启用 CAF 广告插播支持,例如向 Web 发送方添加对可跳过广告的支持。
现在,除了支持 CAF 广告插播之外,还需要在从发送方发送到接收方的 CAF MediaInfo 对象中包含 DAI 流数据。您可以通过多种不同的方式选择发送此数据,例如,您可以对接收器上的 contentSourceID 进行硬编码,并仅通过 MediaInfo 对象发送 videoID;也可以向接收器发送任意标识符,然后从接收器查询在线数据库以获取实际的 DAI 参数。
不过,为简单起见,所有示例都将假定您为 MediaInfo 对象遵循以下简单结构和命名惯例:
contentId
|
相应媒体内容的唯一标识符
|
contentUrl
|
如果 DAI StreamRequest 因任何原因失败,则加载的后备直播网址
|
streamType
|
对于直播,此值应设置为“LIVE”。对于 VOD 流,此值应设置为“BUFFERED”
|
customData
|
assetKey
|
仅限直播。标识要加载的直播
|
contentSourceId
|
仅限 VOD 视频流。标识包含所请求的媒体流的媒体 Feed。
|
videoId
|
仅限 VOD 视频流。用于标识指定媒体 Feed 中的所请求的视频流。
|
ApiKey
|
一个可选的 API 密钥,可能需要该密钥才能从 IMA DAI SDK 中检索视频流网址。
|
senderCanSkip
|
一个布尔值,用于让接收方知道发送设备是否能够显示“跳过”按钮,从而支持可跳过式广告
|
直播示例:
{
"media": {
"contentId": "bbb",
"contentUrl": "https://storage.googleapis.com/interactive-media-ads/media/bbb.m3u8",
"streamType": "LIVE",
"customData": {
"assetKey": "sN_IYUG8STe1ZzhIIE_ksA",
"ApiKey": "",
"senderCanSkip": true
}
},
"credentials": "testCredentials"
}
VOD 视频流示例:
{
"media": {
"contentId": "tos",
"contentUrl": "https://storage.googleapis.com/interactive-media-ads/media/tos.m3u8",
"streamType": "BUFFERED",
"customData": {
"contentSourceId": "2548831",
"videoId": "tears-of-steel",
"ApiKey": "",
"senderCanSkip": true
}
},
"credentials": "testCredentials"
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eChromecast's CAF DAI SDK leverages the Cast Application Framework for seamless integration and bidirectional communication with IMA, reducing sender app customization.\u003c/p\u003e\n"],["\u003cp\u003eCAF Ad Breaks functionality is used for communication, eliminating the need for custom message systems.\u003c/p\u003e\n"],["\u003cp\u003eTo enable CAF DAI, include DAI stream data within the CAF MediaInfo object sent from the sender to the receiver.\u003c/p\u003e\n"],["\u003cp\u003eThe MediaInfo object should follow a structure that uses \u003ccode\u003econtentId\u003c/code\u003e, \u003ccode\u003econtentUrl\u003c/code\u003e, \u003ccode\u003estreamType\u003c/code\u003e, and \u003ccode\u003ecustomData\u003c/code\u003e fields with specific values for live and VOD streams.\u003c/p\u003e\n"],["\u003cp\u003eSender apps might require minor code additions, like skippable ad support for web senders, for full CAF Ad Breaks compatibility.\u003c/p\u003e\n"]]],[],null,["# Write sender apps for the CAF DAI SDK\n\nUnlike previous integrations between Chromecast and DAI, the CAF DAI SDK focuses heavily on maintaining the standards and expectations set by the [Cast Application Framework](https://developers.google.com/cast/docs/developers). As such, there are no requirements to heavily customize your sender apps to manage bidirectional communications between the IMA and the sender app. These communications are now managed via the integrated [CAF Ad Breaks](https://developers.google.com/cast/docs/web_receiver/ad_breaks) functionality, rather than requiring custom message systems to communicate between the sender app and receiver.\n\nDepending on your sender, you may still need to add some limited code to your senders to fully enable CAF Ad Breaks support, such as [adding support for skippable ads to web senders](https://developers.google.com/cast/docs/web_sender/advanced#sending_media_messages_to_the_receiver).\n\nAll that is required now, aside from support for CAF Ad Breaks, is to include the DAI stream data in the [CAF MediaInfo object](https://developers.google.com/cast/docs/reference/web_sender/chrome.cast.media.MediaInfo) that is sent from the sender to the receiver. You can choose to send this data in many different ways, for example, you could hard code the contentSourceID on your receiver and only send videoID with the MediaInfo object, or you could send an arbitrary identifier to the receiver, and from there query an online database for the actual DAI parameters.\n\nHowever, for the purpose of simplicity, all our samples will assume that you follow this simple structure and naming convention for your MediaInfo objects:\n\n|----------------|---------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|\n| **contentId** || A unique identifier for this media item |\n| **contentUrl** || The fallback stream url to load if the DAI StreamRequest fails for any reason |\n| **streamType** || For live streams this value should be set to 'LIVE'. For VOD streams, this value should be set to 'BUFFERED' |\n| **customData** | **assetKey** | **Live streams only.** Identifies the live stream to be loaded |\n| **customData** | **contentSourceId** | **VOD streams only.** Identifies the media feed that contains the requested stream. |\n| **customData** | **videoId** | **VOD streams only.** Identifies the requested stream within the specified media feed. |\n| **customData** | **ApiKey** | An optional API key that can be required to retrieve the stream url from the IMA DAI SDK. |\n| **customData** | **senderCanSkip** | A boolean value to let the receiver know whether the sending device has the ability to display a skip button, enabling support for skippable ads |\n\n**Example LIVE Stream:** \n\n {\n \"media\": {\n \"contentId\": \"bbb\",\n \"contentUrl\": \"https://storage.googleapis.com/interactive-media-ads/media/bbb.m3u8\",\n \"streamType\": \"LIVE\",\n \"customData\": {\n \"assetKey\": \"sN_IYUG8STe1ZzhIIE_ksA\",\n \"ApiKey\": \"\",\n \"senderCanSkip\": true\n }\n },\n \"credentials\": \"testCredentials\"\n }\n\n**Example VOD Stream:** \n\n {\n \"media\": {\n \"contentId\": \"tos\",\n \"contentUrl\": \"https://storage.googleapis.com/interactive-media-ads/media/tos.m3u8\",\n \"streamType\": \"BUFFERED\",\n \"customData\": {\n \"contentSourceId\": \"2548831\",\n \"videoId\": \"tears-of-steel\",\n \"ApiKey\": \"\",\n \"senderCanSkip\": true\n }\n },\n \"credentials\": \"testCredentials\"\n }"]]