屏幕共享功能主要運(yùn)用于遠(yuǎn)程指導(dǎo),遠(yuǎn)程協(xié)助,遠(yuǎn)程教育等場(chǎng)景,我利用聲網(wǎng)的SDK進(jìn)行了實(shí)現(xiàn),詳細(xì)過(guò)程做如下分享,希望能給有需要的人帶來(lái)一定的幫助和啟發(fā),也歡迎同仁進(jìn)行優(yōu)化和討論!
1.接入實(shí)時(shí)通話進(jìn)行過(guò)程控制
屏幕共享的過(guò)程是需要一定的流程,所以我也利用聲網(wǎng)的pod 'AgoraRtm_iOS','1.5.0',這個(gè)SDK就行過(guò)程串聯(lián)和通信。之所以用到此版本,是因?yàn)闆](méi)有接入推送功能,在交互的過(guò)程中需要被請(qǐng)求共享屏幕的一方同意即使響應(yīng)。掛斷過(guò)程也需要告知另一方!因?yàn)椴皇侵攸c(diǎn)所以不做詳細(xì)說(shuō)明。
注意點(diǎn)有以下幾個(gè)方面:
a:pod 'AgoraRtm_iOS','1.5.0',這個(gè)版本才包含對(duì)方是否在線的判斷方法。
b:因?yàn)橐陆尤?pod 'AgoraRtcEngine_iOS', :subspecs => ['RtcBasic', 'ReplayKit'] 屏幕共享用到的SDK,所以有沖突,解決辦法如下如何處理同時(shí)集成 RTM SDK 和 RTC SDK 遇到的問(wèn)題?
2.屏幕共享的實(shí)現(xiàn)
由于 Apple 不支持在 App 主進(jìn)程采集屏幕,因此你需要為屏幕共享流單獨(dú)創(chuàng)建一個(gè) Extension,并在 Extension 中使用 iOS 原生的 ReplayKit 框架實(shí)現(xiàn)錄制屏幕,然后將屏幕共享流發(fā)送給主進(jìn)程、實(shí)現(xiàn)屏幕共享。
注意:受系統(tǒng)限制,屏幕共享只支持 iOS 12.0 及以上版本。該功能對(duì)設(shè)備性能要求較高,聲網(wǎng)推薦你使用 iPhone X 及以上設(shè)備。
前提條件
在實(shí)現(xiàn)屏幕共享前,請(qǐng)確保已在你的項(xiàng)目中實(shí)現(xiàn)基本的實(shí)時(shí)音視頻功能。詳見(jiàn)實(shí)現(xiàn)音視頻互動(dòng)
項(xiàng)目設(shè)置
按照以下步驟修改你的項(xiàng)目文件。
前往你的項(xiàng)目文件夾,用 Xcode 打開?.xcodeproj?文件。
點(diǎn)擊?File > New > Target..., 在彈出的窗口中選擇?Broadcast Upload Extension, 點(diǎn)擊?Next。

在彈出的窗口中填寫?Product Name?等信息,取消勾選?Include UI Extension,點(diǎn)擊?Finish。Xcode 會(huì)自動(dòng)創(chuàng)建該 Extension 的文件夾,其中包含?SampleHandler文件。

修改項(xiàng)目設(shè)置以實(shí)現(xiàn)屏幕共享的代碼邏輯,根據(jù)實(shí)際業(yè)務(wù)需求選擇以下三種方式其中之一即可:
如果你只需使用聲網(wǎng)提供的?YBRExtension.xcframework?動(dòng)態(tài)庫(kù)中的功能,則:選中?Target?為剛剛創(chuàng)建的 Extension,在?Info?中將?NSExtension > NSExtensionPrincipalClass?所對(duì)應(yīng)的?Value?從?SampleHandler?改為?AgoraReplayKitHandler。

你的pod 需要這么寫
target 'YBR*****istant' do
? # Comment the next line if you don't want to use dynamic frameworks
#? use_frameworks!
? # Pods for YBR*****istan
? ? #屏幕共享
? pod'AgoraRtcEngine_iOS', :subspecs => ['RtcBasic', 'ReplayKit']
? #即時(shí)通訊
? pod'AgoraRtm_iOS','1.5.0'
end
target 'YBRExtension' do
? pod'AgoraRtcEngine_iOS', :subspecs => ['RtcBasic', 'ReplayKit']
end
實(shí)現(xiàn)屏幕共享
- (void)startRemote {
//? ? // 視頻默認(rèn)禁用,你需要調(diào)用 enableVideo 啟用視頻流。
? ? [self.mRtcEngine enableVideo];
? ? [self.mRtcEngine enableLocalAudio:NO];
? ? AgoraRtcChannelMediaOptions *options = [AgoraRtcChannelMediaOptions new];
? ? //// 視頻通話場(chǎng)景下,設(shè)置頻道場(chǎng)景為 BROADCASTING。
? ? options.channelProfile = AgoraChannelProfileLiveBroadcasting;
? ? // 將用戶角色設(shè)置為 BROADCASTER。
? ? options.clientRoleType = AgoraClientRoleBroadcaster;
//加入頻道
? ? [self.mRtcEngine joinChannelByToken:[RtmMessageManager shared].currentSession.rtcToken.token channelId:[RtmMessageManager shared].currentSession.rtcToken.channelName uid:YBRuserModel.ybrid.integerValue mediaOptions:options joinSuccess:^(NSString * _Nonnull channel, NSUInteger uid, NSInteger elapsed) {
? ? ? ? if ([[RtmMessageManager shared].currentSession.mineRoler isEqualToString:RemoteRolerPARTICIPANT]) {
? ? ? ? ? ? self.screenParams = [AgoraScreenCaptureParameters2 new];
? ? ? ? ? ? AgoraScreenVideoParameters *videoParams = [AgoraScreenVideoParameters new];
? ? ? ? ? ? //設(shè)置屏幕錄制的分辨率
? ? ? ? ? ? videoParams.dimensions=CGSizeMake(SCREEN_WIDTH*[UIScreen mainScreen].scale,SCREEN_HEIGHT*[UIScreen mainScreen].scale);
? ? ? ? ? ? videoParams.frameRate = AgoraVideoFrameRateFps15;
? ? ? ? ? ? videoParams.bitrate = AgoraVideoBitrateStandard;
? ? ? ? ? ? self.screenParams.videoParams= videoParams;
? ? ? ? ? ? //// 開啟屏幕采集
? ? ? ? ? ? [self.mRtcEngine startScreenCapture:self.screenParams];
? ? ? ? ? ? AgoraRtcChannelMediaOptions *options = [AgoraRtcChannelMediaOptions new];
? ? ? ? ? ? //// 視頻通話場(chǎng)景下,設(shè)置頻道場(chǎng)景為 BROADCASTING。
? ? ? ? ? ? options.channelProfile = AgoraChannelProfileLiveBroadcasting;
? ? ? ? ? ? // 將用戶角色設(shè)置為 BROADCASTER。
? ? ? ? ? ? options.clientRoleType = AgoraClientRoleBroadcaster;
? ? ? ? ? ? //發(fā)布屏幕錄制
? ? ? ? ? ? options.publishScreenCaptureVideo = YES;
? ? ? ? ? ? options.publishCameraTrack=NO;
? ? ? ? ? ? [self.mRtcEngine updateChannelWithMediaOptions:options];
? ? ? ? }
? ? }];
}
使用 Apple 在 iOS 12.0 中新增的?RPSystemBroadcastPickerView,使 App 界面彈出 “開啟屏幕共享” 的按鈕,提示用戶通過(guò)點(diǎn)擊該按鈕開啟錄制,示例代碼如下:
RPSystemBroadcastPickerView *systemBroadcastPicker = [[RPSystemBroadcastPickerView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
? ? systemBroadcastPicker.showsMicrophoneButton=false;
? ? systemBroadcastPicker.autoresizingMask? =UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;
? ? ? ? NSString*bundleId = [NSBundlemainBundle].bundleIdentifier;
? ? ? ? systemBroadcastPicker.preferredExtension= [NSStringstringWithFormat:@"%@.Agora-ScreenShare-Extension",bundleId];
? ? self.systemBroadcastPicker= systemBroadcastPicker;
? ? for (UIView *view in self.systemBroadcastPicker.subviews) {
? ? ? ? if([viewisKindOfClass:[UIButtonclass]]) {
? ? ? ? ? ? [((UIButton *)view) sendActionsForControlEvents:(UIControlEventAllEvents)];
? ? ? ? ? ? break;
? ? ? ? }
? ? }

