Google Meet 即時分享共同觀看 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 替換成活動的媒體標題。
管理目前狀態
當使用者在應用程式中採取行動時,應用程式應該要立即呼叫提供的 API 方法。
您應該只呼叫這些方法以回應重大事件。舉例來說,您不需要在每次應用程式開始播放影片時呼叫這些 API。您在上述情況下建立的 CoWatchingDelegate
會處理在這類情況下取得更新的播放位置。
您可以透過以下方法控制共同觀看狀態:
CoWatchingClient.notifySwitchToMedia
:每當正在播放媒體變更時呼叫。例如,使用者選取新影片,或自動播放下一部影片。CoWatchingClient.notifyPauseState
:在使用者暫停或取消暫停播放的媒體時呼叫。CoWatchingClient.notifySeekToTimestamp
:在使用者明確變更播放位置時呼叫。CoWatchingClient.notifyPlayoutRate
:在使用者將播放速度更新為新的值 (例如 1.25x) 時呼叫。CoWatchingClient.notifyBuffering
:如果使用者的應用程式因先前媒體切換、媒體跳轉或網路壅塞而開始進行緩衝作業,就會呼叫此方法。CoWatchingClient.notifyReady
:在緩衝處理完成後呼叫,且媒體已準備好播放。