实现 Co-Watching API

Google Meet Live Share Co-Watching API 可管理多位参与者在您的应用中观看或收听内容的会议体验。

本指南介绍了如何实现 Co-Watching API。

创建 CoWatchingClient

首先,根据您在使用入门中创建的 AddonSession 创建一个 createCoWatchingClient

如需创建 CoWatchingCient,请调用 AddonSession.createCoWatchingClient 方法并提供 CoWatchingDelegate

每当应用有可用的新状态时,Co-Watching API 都会通过 CoWatchingDelegate 更新应用。当调用 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 可处理更新后的播放位置。

您可以通过以下方式控制“一起看”状态: