Google Meet 实时共享 Co-Watching API 可以管理多个参与者在您的应用中观看或收听内容的会议体验。
本指南介绍了如何实现 Co-Watching API。
创建 CoWatchingClient
首先,请基于您在使用入门中创建的 AddonSession
创建一个 createCoWatchingClient
。
如需创建 CoWatchingCient
,请调用 AddonSession.createCoWatchingClient
方法并提供 CoWatchingDelegate
。
CoWatchingDelegate
是 Co-Watching API 在应用有新状态时更新应用的方式。在调用 CoWatchingDelegate.onCoWatchingStateChanged
方法时,应用应立即应用新状态。
以下代码示例展示了如何使用 Co-Watching API:
TypeScript
const coWatchingClient = await addonSession.createCoWatchingClient({
activityTitle: "ACTIVITY_TITLE",
onCoWatchingStateQuery() {
// This function should return the current state of your CoWatching activity
return getMyApplicationCoWatchingState();
},
onCoWatchingStateChanged(coWatchingState: CoWatchingState) {
// This function should apply newState to your ongoing CoWatching activity
},
});
将 ACTIVITY_TITLE 替换为您的 activity 的媒体标题。
管理当前状态
当用户在您的应用中执行操作时,应用应立即调用所提供的 API 方法。
您应仅在发生重要事件时调用这些方法。例如,您无需在每次应用播放正在播放的视频时都调用这些方法。在这种情况下,上面创建的 CoWatchingDelegate
会处理获取更新后的播放位置。
您可以使用以下方法控制“一起看”状态:
CoWatchingClient.notifySwitchToMedia
:每当正在播放的媒体发生变化时调用。例如,用户选择新视频,或自动播放开始播放下一个视频。CoWatchingClient.notifyPauseState
:在用户暂停或取消暂停正在播放的媒体时调用。CoWatchingClient.notifySeekToTimestamp
:在用户明确更改播放位置时调用。CoWatchingClient.notifyPlayoutRate
:在用户将播放速度更新为新值(例如 1.25 倍)时调用。CoWatchingClient.notifyBuffering
:当用户的应用因之前的媒体切换、媒体跳转或网络拥塞而开始缓冲时调用。CoWatchingClient.notifyReady
:在缓冲完成且媒体现在可供播放时调用。