iOS录屏直播(二)Broadcast Upload Extension和Broadcast Setup UI Extension

									Morris_
									2019.06.13

上一篇总结了ReplayKit相关的知识点,实现了应用内的录屏功能,同时涉及到了很少一部分Broadcast Upload Extension和Broadcast Setup UI Extension的东西,这篇总结Broadcast Upload Extension和Broadcast Setup UI Extension在iOS录屏直播中的运用。

通过ReplayKit的RPScreenRecorder可以实现应用内录屏,在iOS10上录制完还可以编辑和查看录制内容,在iOS11上可以直接边录制一遍获取到音视频数据sampleBuffer。但是没办法实现应用外录屏功能。

要想实现这个手机的录屏,并不受App限制,比如王者荣耀录屏,直接直播到斗鱼App上这种需求,就需要Broadcast Upload Extension和Broadcast Setup UI Extension的支持,最起码的需要Broadcast Upload Extension。iOS10苹果推出Broadcast Upload Extension和Broadcast Setup UI Extension来支持手机的录屏直播功能。

Extension是什么

Extension是扩展,做iOS开发的对这个并不陌生,我们经常对类进行扩展,扩展出分类来,即Category。对App的扩展使用的比较少,在录屏直播功能中我们需要使用App的扩展Broadcast Upload Extension和Broadcast Setup UI Extension。

Broadcast Upload Extension和Broadcast Setup UI Extension

Broadcast Upload Extension和Broadcast Setup UI Extension都是对录屏功能的扩展。在我们的App中,在TARGET下,点击“+”,选择iOS下的Broadcast Upload Extension,然后创建一个新的Target,XXBroadcastUploadExtension。创建时,可以选择同时创建Broadcast Setup UI Extension。

这两个对应用的扩展就被添加在了我们主TARGET下,同时在项目中会生成对应的文件,这些文件是有用的。

我们知道每个TARGET是对应一个工程,我们新创建的两个Terget,引入了两个新的工程,也就是两个新的App,这两个新的App是依赖于我们的App的,随着我们的App的安装也会被安装,随着我们App的卸载被卸载。我们把这两个子target叫做子App,把我们的App叫做宿主App,子App是依赖于宿主App的。

在录屏直播中,虽然我们的App是主App,但是宿主App可单独运行的。在添加Broadcast Upload Extension后,在手机的录屏上,会出现我们的扩展Broadcast Upload Extension。

在这里插入图片描述

Broadcast Upload Extension

创建Broadcast Upload Extension后,项目里会生成一个类SampleHandler,这个类继承RPBroadcastSampleHandler并需要实现了父类的几个方法。

开始录屏该方法会被执行

- (void)broadcastStartedWithSetupInfo:(NSDictionary<NSString *,NSObject *> *)setupInfo {
    // User has requested to start the broadcast. Setup info from the UI extension can be supplied but optional.
}

开始录屏,这个方法会被执行,如果实现了Broadcast Setup UI Extension,里面有个setupInfo,这里的setupInfo就是从Broadcast Setup UI Extension中传过来的,如果没实现Broadcast Setup UI Extension,这里的setupInfo会是空的。

对于录屏直播功能来说,不实现Broadcast Setup UI Extension也可以,Broadcast Upload Extension是必须要实现的,否则,没办法没拿屏幕数据,也就没法直播。

暂停屏该方法会被执行

- (void)broadcastPaused {
    // User has requested to pause the broadcast. Samples will stop being delivered.
}

继续录屏该方法会被执行

- (void)broadcastResumed {
    // User has requested to resume the broadcast. Samples delivery will resume.
}

结束录屏该方法会被执行

- (void)broadcastFinished {
    // User has requested to finish the broadcast.
}

录屏中该方法会被执行,通过这个方法将录屏数据返回到这里

- (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType {
    
}

Broadcast Setup UI Extension

创建这个扩展会生成一个继承UIVieController的BroadcastSetupViewController,在这个contrller里你可以自己添加一些按钮啊输入框之类的东西,它就是一个简单的contrller,不要想的太负责。其具体作用就是,在开始录屏的时候,弹出来这个controller,让用户如输入一些用户名啊什么的,然后把这些信息通过以下方法,通过setupInfo传到Broadcast Upload Extension的broadcastStartedWithSetupInfo这个方法里,然后开始录屏推流。

// Call this method when the user has finished interacting with the view controller and a broadcast stream can start
- (void)userDidFinishSetup {
    
    // URL of the resource where broadcast can be viewed that will be returned to the application
    NSURL *broadcastURL = [NSURL URLWithString:@"http://apple.com/broadcast/streamID"];
    
    // Dictionary with setup information that will be provided to broadcast extension when broadcast is started
    NSDictionary *setupInfo = @{ @"broadcastName" : @"example" };
    
    // Tell ReplayKit that the extension is finished setting up and can begin broadcasting
    [self.extensionContext completeRequestWithBroadcastURL:broadcastURL setupInfo:setupInfo];
}

这个注释注意一下:Call this method when the user has finished interacting with the view controller and a broadcast stream can start 在这个界面上起码得有一个开始直播的操作吧,开始直播后调用userDidFinishSetup这个方法将数据传给Broadcast Upload Extension。

Broadcast Setup UI Extension可以不要,但是也不是多余的,某些用户可能有这个需求,直播前先填写直播房间id,昵称什么的,然后开始直播,这种就需要Broadcast Setup UI 这个Extension了。反正是个controller,界面自己怎么定义都行。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Morris_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值