实现 Co-Watching API

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 会处理获取更新后的播放位置。

您可以使用以下方法控制“一起看”状态: