保存和加载广告连播书签
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本指南介绍了如何使用 IMA DAI SDK 实现书签功能
。
这里假定一个有效的 IMA DAI 实现(如
开始使用。
什么是添加书签?
书签是指先保存书签,然后再返回到特定位置
。假设一位用户观看了 5 分钟的内容
离开视频流,然后返回视频流。书签功能可保存
用户在视频流中的位置,以便流式传输能够从其位置
从而为观看者提供顺畅的体验
DAI 书签功能探秘
为 DAI 视频流添加书签时,您必须记录视频流 ID 和时间
当用户离开视频时触发。当用户返回时,系统会重新请求
流式传输并跳转至保存的时间。由于请求的
只需保存视频流即可设置不同时长的广告插播时间点
时间不会行。您真正想做的是
内容时间。
需要解决的转换方法
IMA DAI SDK 提供了两种方法来请求内容时间
特定直播时间以及特定内容的直播时间
时间。借助这些转换方法,您可以存储已添加书签的
content time,然后定位到
流的新实例。方法如下所示,包括一个链接
到一个示例应用,该应用展示了有效的书签添加实现。
保存书签
在活动暂停后保存书签。
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.contentPlayer pause];
// Ignore this if you're presenting a modal view (e.g. in-app clickthrough).
if ([self.navigationController.viewControllers indexOfObject:self] ==
NSNotFound) {
NSTimeInterval contentTime =
[self.streamManager contentTimeForStreamTime:CMTimeGetSeconds(
self.contentPlayer.currentTime)];
self.video.savedTime = contentTime;
...
}
}
}
正在加载书签
在重新请求流式传输时加载书签。它是实现
VideoStreamPlayer
接口。
- (void)streamManager:(IMAStreamManager *)streamManager didReceiveAdEvent:(IMAAdEvent *)event {
...
case kIMAAdEvent_STREAM_LOADED: {
if (self.video.savedTime > 0) {
NSTimeInterval streamTime =
[self.streamManager streamTimeForContentTime:self.video.savedTime];
[self.IMAVideoDisplay.playerItem
seekToTime:CMTimeMakeWithSeconds(streamTime, NSEC_PER_SEC)];
self.video.savedTime = 0;
}
}
}
示例应用
示例应用
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eThis guide explains how to implement bookmarking in video-on-demand (VOD) streams using the IMA DAI SDK for a seamless viewing experience.\u003c/p\u003e\n"],["\u003cp\u003eBookmarking involves saving the user's content time, not just the stream time, to ensure accurate resumption upon return.\u003c/p\u003e\n"],["\u003cp\u003eThe IMA DAI SDK provides methods to convert between stream time and content time, which are crucial for bookmarking functionality.\u003c/p\u003e\n"],["\u003cp\u003eSaving bookmarks involves recording the content time when the user pauses the video, while loading bookmarks requires seeking to the corresponding stream time in the new stream instance.\u003c/p\u003e\n"],["\u003cp\u003eA sample app demonstrating bookmarking implementation is available on GitHub.\u003c/p\u003e\n"]]],["Bookmarking in Dynamic Ad Insertion (DAI) involves saving a user's position in a video stream and resuming from that point. Instead of saving stream time, the system records the content time using conversion methods provided by the IMA DAI SDK. When the user leaves, the content time is saved. Upon return, the saved content time is used to calculate the new stream time, allowing seeking to the correct point in the stream. The process is demonstrated with sample code and a provided sample application.\n"],null,["# Save and load ad stream bookmarks\n\nThis guide shows how to implement bookmarking using the IMA DAI SDK\nwhen using Dynamic Ad Insertion (DAI) for video-on-demand (VOD) streams.\nThis assumes a working IMA DAI implementation, such as the one presented in\n\n\n[Get Started](/interactive-media-ads/docs/sdks/ios/dai-quickstart).\n\n\nWhat is bookmarking?\n--------------------\n\nBookmarking is the ability to save and then return to a specific point\nin the content stream. Suppose a user watches five minutes of content,\nleaves the video stream, and then returns to it. Bookmarking saves the\nuser's position in the stream so the stream can pick up from where it\nleft off, providing a seamless experience to the viewer.\n\nDAI bookmarking under the hood\n------------------------------\n\nWhen bookmarking a DAI stream, you must record the stream id and time\nwhen the user leaves the video. When the user returns, re-request the\nstream and seek to the saved time. Since each instance of the requested\nstream can have ad breaks of different durations simply saving the stream\ntime won't work. What you really want to do is continue from the same\n**content time**.\n\nConversion methods to the rescue\n--------------------------------\n\nThe IMA DAI SDK provides a pair of methods to request the **content time**\nfor a given **stream time** and the **stream time** for a given **content\ntime** . Using these conversion methods you can store the bookmarked\n**content time** and then seek to the corresponding **stream time** in\nthe new instance of the stream. Here's the approach, including a link\nto a sample app that shows a working bookmarking implementation.\n\nSaving bookmarks\n----------------\n\nSave a bookmark when the Activity is paused. \n\n - (void)viewWillDisappear:(BOOL)animated {\n [super viewWillDisappear:animated];\n [self.contentPlayer pause];\n // Ignore this if you're presenting a modal view (e.g. in-app clickthrough).\n if ([self.navigationController.viewControllers indexOfObject:self] ==\n NSNotFound) {\n NSTimeInterval contentTime =\n [self.streamManager contentTimeForStreamTime:CMTimeGetSeconds(\n self.contentPlayer.currentTime)];\n self.video.savedTime = contentTime;\n ...\n }\n }\n }\n\nLoading bookmarks\n-----------------\n\nLoad the bookmark when re-requesting a stream. It's part of implementing\nthe `VideoStreamPlayer` interface. \n\n - (void)streamManager:(IMAStreamManager *)streamManager didReceiveAdEvent:(IMAAdEvent *)event {\n ...\n case kIMAAdEvent_STREAM_LOADED: {\n if (self.video.savedTime \u003e 0) {\n NSTimeInterval streamTime =\n [self.streamManager streamTimeForContentTime:self.video.savedTime];\n [self.IMAVideoDisplay.playerItem\n seekToTime:CMTimeMakeWithSeconds(streamTime, NSEC_PER_SEC)];\n self.video.savedTime = 0;\n }\n }\n }\n\nSample app\n----------\n\n[Sample app](//github.com/googleads/googleads-ima-ios-dai)"]]