Co-Doing API を実装する

このページでは、Co-Doing API を使用して共同作業をサポートする方法について説明します。 説明します。

初期設定

ライブラリを使用できるように準備するために、ライブ共有アプリケーションで CoDoingClient 共同作業セッションを表すオブジェクトです。

Meet ライブ共有 SDK を使用するには、 AddonClientFactory.getClient メソッドを呼び出します。このメソッドは、 AddonClient 共同行動セッションへのエントリー ポイントとして機能します。

クライアントを使用するには、 newSessionBuilder メソッドを AddonClient から使用して、新しいジョブのビルダーを返す AddonSessionnewSessionBuilder は、 AddonSessionHandler インターフェースを使用して、 追加する必要があります。

セッションを開始するには、 withCoDoing メソッドをビルダーに追加します。

次のコードサンプルは、共同実行クライアントの基本的な初期化を示しています。 オブジェクト:

Java

class AwesomeVideoAddonSessionHandler implements AddonSessionHandler {}

//For sample implementation, see the "Handle incoming updates" section.
class AwesomeVideoCoDoingHandler implements CoDoingHandler {}

public ListenableFuture<AddonSession> initialSetup() {
  AddonClient meetClient = AddonClientFactory.getClient();
  return meetClient
      .newSessionBuilder(
          new AwesomeVideoAddonSessionHandler())
      .withCoDoing(new AwesomeVideoCoDoingHandler())
      .begin();
}

動画を一時停止

ライブ共有に参加しているときにユーザーが再生を一時停止した場合 使用する場合は、ライブ ストリームの参加者全員が 共有機能もオフにします

そのためには、まず CoDoingState 動画が一時停止されたことを示すメッセージと、ブロードキャストするよう Google Meet に指示する 他のすべての参加者に setGlobalState メソッドを呼び出します。共有されたグローバル状態は、すべての参加者のデフォルトの状態になります。 新しいステータスが設定されるまで、このログが保持されます。

次のコードサンプルは、ユーザーに一時停止状態を通知する方法を示しています。

Java

public void onVideoPaused(String videoUrl, Instant currentTimestamp) {
  // Create an internal state object to share with other participants. Note: It's
  // good practice to encode all metadata—even seemingly irrelevant data—into
  // ActivityState updates to guard against race conditions and other subtle
  // failures.
  AwesomeVideoState videoState = AwesomeVideoState
    .builder()
    .videoUrl(videoUrl)
    .videoTimestamp(currentTimestamp)
    .isPaused(true)
    .build();

  // Create the CoDoingState object to wrap the internal state
  CoDoingState coDoingState = new CoDoingState();
  coDoingState.state = SerializationUtils.serialize(videoState);

  // Use Meet to broadcast internal state update to all other participants
  this.coDoingClient.setGlobalState(coDoingState);
};

コードサンプルは、シリアル化された videoState オブジェクトをトリガーしてブロードキャストします。 その他すべての Meet インスタンスがライブ共有に参加します。 体験できます他のユーザーからブロードキャストの最新情報を受け取る方法について詳しくは、 受け取った更新を処理するをご覧ください。 できます。

次の図は、一時停止アクションが実行された後のイベント シーケンスを示しています。 発生済み:

Live Sharing API の開始の図。

動画の一時停止を解除

動画の一時停止と同様に、ユーザーが自分のデバイスで動画の一時停止を解除した場合 Meet は、この操作を他のライブ グループにブロードキャストする必要があります。 方法について説明します。

送信者側(動画の一時停止を解除したユーザー)には、 一時停止の例として、isPaused ステータスの更新があります。

次のコードサンプルは、一時停止されていない状態であることを、 :

Java

public void onVideoUnpaused(String videoUrl, Instant currentTimestamp) {
  AwesomeVideoState videoState = AwesomeVideoState
    .builder()
    .videoUrl(videoUrl)
    .videoTimestamp(currentTimestamp)
    .isPaused(false)
    .build();

  CoDoingState coDoingState = new CoDoingState();
  coDoingState.state = SerializationUtils.serialize(videoState);

  this.coDoingClient.setGlobalState(coDoingState);
}

動画に移動

動画の一時停止一時停止の解除と同様に、 ユーザーがローカルアプリのタイムラインを新しいタイムスタンプにドラッグすると、 Meet では、この操作を参加者全員に配信する必要があります。

次のコードサンプルは、更新されたタイムスタンプをユーザーに通知する方法を示しています。 考えてみましょう

Java

public void onVideoSeeked(String videoUrl, Instant currentTimestamp, bool isPaused) {
  AwesomeVideoState videoState = AwesomeVideoState
    .builder()
    .videoUrl(videoUrl)
    .videoTimestamp(currentTimestamp)
    .isPaused(isPaused)
    .build();

  CoDoingState coDoingState = new CoDoingState();
  coDoingState.state = SerializationUtils.serialize(videoState);

  this.coDoingClient.setGlobalState(coDoingState);
}

別の動画を再生

ユーザーが YouTube の別の動画を選択して視聴中の動画を変更し、 ライブ共有を行う場合、Meet で新しい動画が再生される必要があります。 できます。変更された動画は videoState.videoUrl に保存されます。

次のコードサンプルは、更新された動画の URL をユーザーに通知する方法を示しています。

Java

public void onVideoChanged(String videoUrl, Duration currentTimestamp, bool isPaused) {
  AwesomeVideoState videoState = AwesomeVideoState
    .builder()
    .videoUrl(videoUrl)
    .videoTimestamp(currentTimestamp)
    .isPaused(isPaused)
    .build();

  CoDoingState coDoingState = new CoDoingState();
  coDoingState.state = SerializationUtils.serialize(videoState);

  this.coDoingClient.setGlobalState(coDoingState);
}

共同行動を終了

ユーザーがアクティビティの終了を選択すると、 endSession メソッドが Meet アプリから切断されます。これによって ユーザーが Meet を終了しても、会議を 説明します。

次のコードサンプルは、停止したセッションをユーザーに通知する方法を示しています。

Java

public void endCoDoing() {
  this.session.endSession();
}

着信アップデートを処理する

他の参加者の Meet アプリがブロードキャストを受信すると、 onGlobalStateChanged() コールバックがトリガーされます。通常 重要なのは 受信アップデートに応じて実行するアクション 動画のタイムスタンプ(ローカルのタイムスタンプと十分に異なる場合)。

次のコードサンプルは、さまざまな受信アップデートを処理する方法を示しています。

Java

class AwesomeVideoCoDoingHandler implements CoDoingHandler {
  public void onGlobalStateChanged(CoDoingState update) {
    AwesomeVideoState videoState = SerializationUtils.deserialize(update.state());

    // Handle transition to new video.
    if (!videoState.videoUrl.equals(this.videoPlayer.videoUrl)) {
      this.videoPlayer.loadVideo(videoState.videoUrl);
    }

    // If the timestamp in the arriving update has sufficiently diverged, adjust
    // the local video playout.
    if (videoState.videoTimestamp.minus(this.videoPlayer.videoTimestamp).abs() >
                                        Duration.ofSeconds(2)) {
      this.videoPlayer.seek(videoState.videoTimestamp);
    }

    // Update pause state, if necessary.
    if (!videoState.isPaused && this.videoPlayer.isPaused) {
      this.videoPlayer.unpause();
    } else if (videoState.isPaused && !this.videoPlayer.isPaused) {
      this.videoPlayer.pause();
    }
  }
}