Co-Watching API 用于管理多个参与者在您的应用中观看或收听内容的会议体验。
本指南介绍了如何实现 Co-Watching API。
开始使用
如需使用 Co-Watching API,您必须先部署 Meet 加载项。完成这些步骤后,您就可以开始在新插件中使用 Co-Watching API 了。
如需使用 Co-Watching API,请先获取一个 AddonSession
对象,该对象是 Google Meet 集体活动的入口点:
TypeScript
const session = await window.meet.addon.createAddonSession({
cloudProjectNumber: "CLOUD_PROJECT_NUMBER",
});
将 CLOUD_PROJECT_NUMBER 替换为您的 Google Cloud 项目的项目编号。
创建一起看客户端
首先,基于 AddonSession
创建 CoWatchingClient
对象。
如需创建 CoWatchingCient
,请调用 createCoWatchingClient()
方法并提供 CoWatchingDelegate
对象。
CoWatchingDelegate
是 Co-Watching API 在有新状态可用时更新应用的方式。预计当 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 替换为活动的媒体标题。
管理当前状态
当用户在您的应用中采取行动时,您的应用应立即调用所提供的 API 方法。
您只应在响应重大事件时调用这些方法。例如,您无需在每次应用播放视频时都调用它们。您创建的 CoWatchingDelegate
会处理以下情况下的播放位置更新。
您可以使用以下方法控制同步观看状态:
notifyBuffering()
:当用户的应用因之前媒体切换、媒体搜索或网络拥塞而开始缓冲时调用。notifyPauseState()
:当用户暂停或取消暂停正在播放的媒体时调用。notifyPlayoutRate()
:当用户将播放速度更新为新值(例如 1.25 倍)时调用。notifyReady()
:在缓冲完成且媒体现在可以播放时调用。notifySeekToTimestamp()
:当用户明确更改播放位置时调用。notifySwitchToMedia()
:每当正在播放的媒体发生变化时调用。例如,用户选择新视频,或自动播放开始播放下一个视频。