最近更新时间:2023.09.25 18:57:34
首次发布时间:2023.06.05 21:38:35
本文介绍如何实现 iOS 开播 SDK 的进阶功能。
您已集成 iOS 开播 SDK。详见 SDK 集成。
主持人可以实时共享屏幕画面,而无需有线或无线连接的繁琐步骤,大大降低了直播门槛。录屏直播适用于游戏直播或演示效果直播等场景。
注意
完成以下步骤实现该功能:
在工程的 Podfile
文件中添加 ScreenShare
依赖。
# 如果只集成开播 SDK pod'BDLive', 'SDK_VERSION', :subspecs => [ 'LiveStreaming', 'ScreenShare' ] # 如果同时集成开播 SDK和观播 SDK pod'BDLive', 'SDK_VERSION', :subspecs => [ 'LiveStreaming', 'Viewer', 'ScreenShare' ] end
将 SDK_VERSION
替换为指定版本号即可。推荐使用最新版本。具体版本号与发版说明,详见iOS 开播 SDK 发布历史。
打开终端窗口,执行 cd
命令进入您的工程目录。执行 pod install
命令安装依赖。
在 Xcode 中,完成以下步骤,创建屏幕共享扩展的 Target。
单击 File > New > Target。
在 iOS 页签下,选择 Broadcast Upload Extension,即屏幕共享扩展。单击 Next。
输入 Target 名称并单击 Finish。
说明
在 Xcode 中完成以下步骤,实现屏幕共享扩展和 App 之间的数据共享。
进入步骤 3 中创建的扩展 Target 中。
单击 Signing & Capabilities 页签并单击 + Capability。
双击对话框中的 App Groups。
单击 App Groups 区域的 +。
说明
如果您有可用的 App Group,可直接选中该 App Group 而无需新建。请将该 App Group 的 Container ID 记录在某个安全的地方。在步骤 6 和 7 中,您必须将该 Container ID 传入 SDK。
在弹出的对话框中,输入以 group.
开头的 Container ID 并单击 OK。
注意
请将该 Container ID 记录在某个安全的地方。在步骤 6 和 7 中,您必须将该 Container ID 传入 SDK。
App Group 新建成功后,会自动在 App Groups 区域中被选中。
进入 App Target 中,重复步骤 4-b 和 4-c。在 App Groups 区域中,选择在扩展 Target 中被选中的 App Group。
详见 Configuring App Groups。
在 Xcode 中,完成以下步骤,实现屏幕采集的逻辑。
VolcEngineRTCScreenCapturer.xcframework
的嵌入方法设置为 Do Not Embed。说明
录屏直播功能仅对 Deployment Info 区域选定版本及以上的 iOS 设备可见。
在步骤 3 中创建的扩展 Target 中,Xcode 自动创建以下文件:SampleHandler.h
和 SampleHandler.m
。在 Xcode 中打开 SampleHandler.m
文件,实现屏幕采集逻辑。示例代码如下所示。
#import "SampleHandler.h" #import <VolcEngineRTCScreenCapturer/ByteRTCScreenCapturerExt.h> @interface SampleHandler () <ByteRtcScreenCapturerExtDelegate> @property (nonatomic, assign) BOOL shouldScreenShare; @end @implementation SampleHandler - (void)broadcastStartedWithSetupInfo:(NSDictionary<NSString *,NSObject *> *)setupInfo { // groupId:步骤 4 中选中的 App Group 的 Container ID [[ByteRtcScreenCapturerExt shared] startWithDelegate:self groupId:xxx]; // 如果主持人在 App 的预览页(即可选择录屏直播的页面)中,屏幕共享扩展将收到 App 发出的 onNotifyAppRunning 回调。如果扩展在 2 秒内没有收到 onNotifyAppRunning 回调,则认为主持人未在 App 的预览页,您应该通过调用 finishBroadcastWithError: 方法停止屏幕采集 // 按需自定义 NSLocalizedFailureReasonErrorKey 的值 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if (!self.shouldScreenShare) { NSDictionary *dic = @{ NSLocalizedFailureReasonErrorKey : @"The host is not in the live room"}; NSError *error = [NSError errorWithDomain:RPRecordingErrorDomain code:RPRecordingErrorUserDeclined userInfo:dic]; [self finishBroadcastWithError:error]; } }); } - (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType { // 混流 switch (sampleBufferType) { case RPSampleBufferTypeVideo: // 采集到的屏幕视频流 case RPSampleBufferTypeAudioApp: // 采集到的设备音频流 [[ByteRtcScreenCapturerExt shared] processSampleBuffer:sampleBuffer withType:sampleBufferType]; // 使用该方法,将采集到的屏幕视频流和设备音频流推送至观看页 break; case RPSampleBufferTypeAudioMic: // 采集到的麦克风音频流 // 开播 SDK 实现了麦克风音频流的采集并将其推送至观看页。因此,您无需在此处理麦克风音频流 break; default: break; } } /// 在主持人停止共享屏幕时触发该回调,通知您停止屏幕采集 - (void)onQuitFromApp { // 按需自定义 NSLocalizedFailureReasonErrorKey 的值 NSDictionary *dic = @{ NSLocalizedFailureReasonErrorKey : @"You stopped sharing the screen"}; NSError *error = [NSError errorWithDomain:RPRecordingErrorDomain code:RPRecordingErrorUserDeclined userInfo:dic]; [self finishBroadcastWithError:error]; } /// App 在后台被终止时触发该回调,通知您停止屏幕采集 - (void)onSocketDisconnect { // 按需自定义 NSLocalizedFailureReasonErrorKey 的值 NSDictionary *dic = @{ NSLocalizedFailureReasonErrorKey : @"Disconnected"}; NSError *error = [NSError errorWithDomain:RPRecordingErrorDomain code:RPRecordingErrorUserDeclined userInfo:dic]; [self finishBroadcastWithError:error]; } /// 检测到 App 正在运行时触发该回调 - (void)onNotifyAppRunning { self.shouldScreenShare = YES; } @end
在 Xcode 的 App Target 中,打开定义如何进入直播间的文件并添加以下代码。详见进入直播间。
if (@available(iOS 12.0, *)) { model.extensionBundleId = @"xxxxx"; // 屏幕共享扩展的 Bundle Identifier model.groupId = @"group.xxxxxx"; // 步骤 4 中选中的 App Group 的 Container ID }