支持可跳过式广告
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
使用 CAF DAI SDK 请求和展示可跳过式广告是一项有点复杂的任务,因为这需要确定使用的发送者和接收者的组合是否能够显示“跳过”按钮并与之互动。
SDK 会自动尽最大努力检测当前接收器是否能够直接呈现“跳过”按钮并与之交互。但是,发布商需要通过开启/关闭 imaRequestData.senderCanSkip
属性,确定是否有关联的发件人可以展示可跳过的广告。
在 CAF DAI SDK 的所有示例中, senderCanSkip 作为媒体对象的一部分直接从发送端传递。这是一个经过刻意简化的实现,适用于单个发送器,但在实际应用中,一个 CAF 接收器可能会附加到多个发送器应用,当加载该媒体对象并发出流请求时,将媒体文件加入队列的发送器可能无法再连接。
更高级的接收器可以跟踪哪些关联发件人类型支持跳过,哪些不支持跳过,并在加载时检查目前是否有任何可以跳过的发件人连接到接收器。
示例(单个发件人):
...
let getStreamRequest = (request) => {
const imaRequestData = request.media.customData;
let streamRequest = null;
if (imaRequestData.assetKey) {
// Live stream
streamRequest = new google.ima.cast.dai.api.LiveStreamRequest();
streamRequest.assetKey = imaRequestData.assetKey;
} else if (imaRequestData.contentSourceId) {
// VOD stream
streamRequest = new google.ima.cast.dai.api.VODStreamRequest();
streamRequest.contentSourceId = imaRequestData.contentSourceId;
streamRequest.videoId = imaRequestData.videoId;
}
if (streamRequest && imarequestdata.ApiKey) {
streamRequest.ApiKey = imarequestdata.ApiKey;
}
if (streamRequest && imarequestdata.senderCanSkip) {
streamRequest.senderCanSkip = imaRequestData.senderCanSkip;
}
return streamRequest;
};
...
示例(支持多发件人):
...
let getStreamRequest = (request) => {
const imaRequestData = request.media.customData;
let streamRequest = null;
if (imaRequestData.assetKey) {
// Live stream
streamRequest = new google.ima.cast.dai.api.LiveStreamRequest();
streamRequest.assetKey = imaRequestData.assetKey;
} else if (imaRequestData.contentSourceId) {
// VOD stream
streamRequest = new google.ima.cast.dai.api.VODStreamRequest();
streamRequest.contentSourceId = imaRequestData.contentSourceId;
streamRequest.videoId = imaRequestData.videoId;
}
if (streamRequest && imarequestdata.ApiKey) {
streamRequest.ApiKey = imarequestdata.ApiKey;
}
let senders = castContext.getSenders();
let senderCanSkip = (sender) => {
const skippableUserAgents = [
"iOS CastSDK",
"Android CastSDK"
];
// trim user agent to just include device
let userAgent = sender.userAgent.explode(',')[0];
return skippableUserAgents.includes(userAgent);
};
if (streamRequest && senders.some(senderCanSkip)) {
streamRequest.senderCanSkip = imarequestdata.senderCanSkip;
}
return streamRequest;
};
...
归根结底,发布商需要对用户的内容负责,同时,由于所有可跳过的发送方都可能会在发送视频流请求与播放广告之间断开连接,因此您可能需要为 STARTED
事件附加一个处理程序,用于检查广告是否可跳过,以及发送方和接收方都无法支持可跳过的广告。如果此类广告出现,该处理程序会自动跳过此类广告。不过,这种高级实现超出了 CAF DAI 支持的范围。
默认情况下,如果出现这种极端情况,用户从技术层面来说仍然可以发起跳过,方法是先连接支持可跳过功能的发送者,然后点击显示的“跳过”按钮。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eSkippable ad functionality in CAF DAI depends on both sender and receiver capabilities, requiring publishers to manage \u003ccode\u003esenderCanSkip\u003c/code\u003e attribute.\u003c/p\u003e\n"],["\u003cp\u003eSingle-sender setups can pass \u003ccode\u003esenderCanSkip\u003c/code\u003e directly, while multi-sender scenarios require receivers to track skip-capable senders.\u003c/p\u003e\n"],["\u003cp\u003ePublishers should handle edge cases where skippable ads play without a skippable sender or receiver for optimal user experience.\u003c/p\u003e\n"],["\u003cp\u003eWhile CAF DAI automatically detects skip button support, publishers are ultimately responsible for managing the skip functionality.\u003c/p\u003e\n"],["\u003cp\u003eIf an ad is skippable but no skippable sender is connected, users can connect one later to enable skipping.\u003c/p\u003e\n"]]],["The CAF DAI SDK's display of skippable ads requires determining if the senders and receivers support skip buttons. The `senderCanSkip` attribute, part of `imaRequestData`, is used to indicate sender capabilities. For single-sender scenarios, this is straightforward. In multi-sender environments, the receiver must track which sender types allow skipping and check for connected skip-capable senders upon loading. Ultimately, publishers handle edge cases where senders disconnect after the request but before the ad plays, potentially by auto-skipping ads.\n"],null,["# Support skippable ads\n\nRequesting and displaying skippable ads with the CAF DAI SDK is a bit of a complex task, because it requires identifying whether the combination of senders and receivers in use have the ability to display and interact with a skip button.\n\nThe SDK will automatically make best efforts to detect whether the current receiver has the ability to render and interact with a skip button directly. However, the publisher needs to determine whether any of the attached senders can display skippable ads, by toggling the `imaRequestData.senderCanSkip` attribute.\n\nIn all of the samples for the CAF DAI SDK, senderCanSkip is passed directly from the sender, as a part of the media object. This is a purposefully simplified implementation that works well for a single sender, but in real-world applications a CAF receiver may be attached to multiple sender apps, and the sender that queues a media file may no longer be connected when that media object is loaded and the stream request is made.\n\nA more advanced receiver could keep track of which affiliated sender types support skipping, and which do not, and would check at load time to see if any skip-capable senders are currently attached to the receiver.\n\n**Example (single-sender):** \n\n ...\n\n let getStreamRequest = (request) =\u003e {\n const imaRequestData = request.media.customData;\n let streamRequest = null;\n if (imaRequestData.assetKey) {\n // Live stream\n streamRequest = new google.ima.cast.dai.api.LiveStreamRequest();\n streamRequest.assetKey = imaRequestData.assetKey;\n } else if (imaRequestData.contentSourceId) {\n // VOD stream\n streamRequest = new google.ima.cast.dai.api.VODStreamRequest();\n streamRequest.contentSourceId = imaRequestData.contentSourceId;\n streamRequest.videoId = imaRequestData.videoId;\n }\n if (streamRequest && imarequestdata.ApiKey) {\n streamRequest.ApiKey = imarequestdata.ApiKey;\n }\n if (streamRequest \\&\\& imarequestdata.senderCanSkip) {\n streamRequest.senderCanSkip = imaRequestData.senderCanSkip;\n }\n return streamRequest;\n };\n ...\n\n**Example (multi-sender support):** \n\n ...\n\n let getStreamRequest = (request) =\u003e {\n const imaRequestData = request.media.customData;\n let streamRequest = null;\n if (imaRequestData.assetKey) {\n // Live stream\n streamRequest = new google.ima.cast.dai.api.LiveStreamRequest();\n streamRequest.assetKey = imaRequestData.assetKey;\n } else if (imaRequestData.contentSourceId) {\n // VOD stream\n streamRequest = new google.ima.cast.dai.api.VODStreamRequest();\n streamRequest.contentSourceId = imaRequestData.contentSourceId;\n streamRequest.videoId = imaRequestData.videoId;\n }\n if (streamRequest && imarequestdata.ApiKey) {\n streamRequest.ApiKey = imarequestdata.ApiKey;\n }\n\n let senders = castContext.getSenders();\n let senderCanSkip = (sender) =\\\u003e {\n const skippableUserAgents = \\[\n \"iOS CastSDK\",\n \"Android CastSDK\"\n \\];\n // trim user agent to just include device\n let userAgent = sender.userAgent.explode(',')\\[0\\];\n return skippableUserAgents.includes(userAgent);\n };\n if (streamRequest \\&\\& senders.some(senderCanSkip)) {\n streamRequest.senderCanSkip = imarequestdata.senderCanSkip;\n }\n return streamRequest;\n };\n\n ...\n\nUltimately, publishers are responsible for their users' experience, and since it is possible for all skippable senders to disconnect between when the stream request is made and when the ad is played, you may want to attach a handler to the `STARTED` event that will check if an ad is skippable and both the senders and receiver are unable to support skippability. That handler could automatically skip such ads, if they appear. Such an advanced implementation, however, is beyond the scope of CAF DAI support.\n\nBy default, if such an edge case occurs, the user is still technically able to initiate a skip, by first connecting a sender that supports skippability and then clicking the skip button that appears."]]