این صفحه نحوه استفاده از Co-Doing API برای پشتیبانی از یک سناریوی همکار را توضیح می دهد.
راه اندازی اولیه
برای آمادهسازی کتابخانه برای استفاده، برنامه اشتراکگذاری زنده باید یک شی CoDoingClient
را که نمایانگر یک جلسه همکار است، مقداردهی اولیه کند.
برای استفاده از Meet Live Sharing SDK، با روش AddonClientFactory.getClient
تماس بگیرید. این یک AddonClient
را برمی گرداند که به عنوان نقطه ورودی برای جلسه همکار عمل می کند.
برای استفاده از کلاینت، متد newSessionBuilder
از AddonClient
فراخوانی کنید تا یک سازنده برای AddonSession
جدید برگرداند. newSessionBuilder
رابط AddonSessionHandler
را برای رسیدگی به تماس های ارائه شده توسط افزونه برای جلسه پیاده سازی می کند.
برای شروع یک جلسه، متد withCoDoing
را به سازنده اضافه کنید.
نمونه کد زیر یک مقدار اولیه اولیه شی کلاینت co-doing را نشان می دهد:
جاوا
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
برای آن پخش کند. حالت جهانی مشترک به حالت پیش فرض برای همه شرکت کنندگان اعم از موجود یا جدید تبدیل می شود تا زمانی که حالت جدیدی تنظیم شود.
نمونه کد زیر نشان می دهد که چگونه کاربران را از وضعیت مکث آگاه کنند:
جاوا
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 که در تجربه اشتراکگذاری زنده شرکت میکنند، پخش شود. برای جزئیات نحوه دریافت بهروزرسانیهای پخش از سایر شرکتکنندگان، بخش مدیریت بهروزرسانیهای ورودی را ببینید.
نمودار زیر دنباله ای از رویدادها را پس از شروع عمل مکث توصیف می کند:
لغو مکث ویدیو
مانند توقف موقت ویدیو ، اگر کاربری ویدیو را در برنامه محلی خود لغو مکث کند، Meet باید این عملیات را برای سایر شرکتکنندگان اشتراکگذاری زنده پخش کند.
در سمت فرستنده (کاربری که ویدیو را لغو مکث می کند)، تنها تفاوت با مثال مکث این است که وضعیت isPaused
به روز می شود.
نمونه کد زیر نشان می دهد که چگونه می توان کاربران را از حالت توقف موقت از سمت فرستنده مطلع کرد:
جاوا
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 باید این عملیات را برای همه شرکتکنندگان پخش کند.
نمونه کد زیر نشان می دهد که چگونه می توان به کاربران از مهر زمانی به روز شده از سمت فرستنده اطلاع داد:
جاوا
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);
}
یک ویدیوی متفاوت پخش کنید
اگر کاربر با انتخاب ویدیوی دیگری در برنامه محلی، ویدیوی در حال تماشا را نیز تغییر دهد، Meet باید ویدیوی جدید را برای همه شرکتکنندگان اشتراکگذاری زنده پخش کند. ویدیوی تغییر یافته در videoState.videoUrl
ذخیره می شود.
نمونه کد زیر نحوه اطلاع کاربران را از URL ویدیوی به روز شده نشان می دهد:
جاوا
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 را مجبور به پایان جلسه میکند و نه باعث میشود کاربر جلسه را ترک کند.
نمونه کد زیر نحوه اطلاع کاربران از جلسه متوقف شده را نشان می دهد:
جاوا
public void endCoDoing() {
this.session.endSession();
}
به روز رسانی های دریافتی را مدیریت کنید
هنگامی که برنامه Meet یک شرکتکننده دیگر پخشی دریافت میکند، پاسخ تماس onGlobalStateChanged()
فعال میشود. معمولاً، مهم است که در مورد اینکه چه اقدامی در پاسخ به بهروزرسانیهای دریافتی انجام دهید، تصمیمات خوبی بگیرید، مانند تنها مطابقت با مهر زمانی ویدیوی دریافتی در صورتی که به اندازه کافی با مهر زمانی محلی متفاوت باشد.
نمونه کد زیر نحوه مدیریت به روز رسانی های مختلف دریافتی را نشان می دهد:
جاوا
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();
}
}
}