ios 10 开发-录屏直播 ReplayKit Live 与 Broadcast UI/Upload Extension
在 iOS 8 及以前,第三方 App 如果想要全局录屏,只能使用私有 API,详见非越狱后台录屏。
升级到 iOS 9 后,官方新增了 ReplayKit,并且禁用了录屏的私有 API。ReplayKit 并不算是完美的录屏方案,如果想要把梦幻西游的游戏过程录制下来,需要梦幻西游这个应用本身添加 ReplayKit 的支持,然后再把录制的视频分享出去。对于不支持 ReplayKit 的游戏,怎么录制?答案是,没有办法。试想,又有多少个游戏会内置 ReplayKit 呢?
iOS 10 在 iOS 9 的 ReplayKit 保存录屏视频的基础上,增加了视频流实时直播功能(streaming live),官方介绍见 Go Live with ReplayKit 。下面详细说说这个流程。
1. ReplayKit Live 概述
从录制到直播,整体流程如下:
- 被录制端需引入 ReplayKit,发起广播请求。
- 广播端需要实现 Broadcast UI 和 Broadcast Upload 两个 App Extension ,以便出现在被录制端的 App 列表。
- 被录制端选定广播端 App 后,开始直播。
其中, 被录制端
可以是任意一个 App,如梦幻西游, 广播端
是支持 ReplayKit Live 的直播平台,如虎牙直播。
2. 被录制端(游戏或应用)的实现
被录制端需要在原有功能的基础上,增加一个唤起广播的入口。代码:
#import <ReplayKit/ReplayKit.h>- (IBAction)onLiveButtonPressed:(id)sender {[RPBroadcastActivityViewController loadBroadcastActivityViewControllerWithHandler:^(RPBroadcastActivityViewController * _Nullable broadcastActivityViewController, NSError * _Nullable error) {self.broadcastAVC = broadcastActivityViewController;self.broadcastAVC.delegate = self;[self presentViewController:self.broadcastAVC animated:YES completion:nil];}];
}#pragma mark - RPBroadcastActivityViewControllerDelegate- (void)broadcastActivityViewController:(RPBroadcastActivityViewController *)broadcastActivityViewController didFinishWithBroadcastController:(nullable RPBroadcastController *)broadcastController error:(nullable NSError *)error {[self.broadcastAVC dismissViewControllerAnimated:YES completion:nil];self.broadcastController = broadcastController;[broadcastController startBroadcastWithHandler:^(NSError * _Nullable error) {if (!error) {self.liveButton.selected = YES;} else {NSLog(@"startBroadcastWithHandler error: %@", error);}}];
}
如下图的右上角就是开始广播的入口按钮。
如果手机内没有支持广播的 App,会弹框提示到 App Store 查找一个。
如果已经安装支持广播的 App,则会列出,点击后会打开广播端的 Broadcast UI。
下面要说的就是怎么实现一个支持广播的 App。
3. 广播端(直播平台)的实现
很多直播 App 本身已经支持通过摄像头进行视频流上传、直播,新增对 ReplayKit Live 的支持,只需要创建两个扩展的 target,分别是 Broadcast UI Extension 和 Broadcast Upload Extension,在XCode 8 中内置了这两个模板。
3.1 Broadcast UI
Broadcast UI 负责广播前的一些初始化工作,比如,让用户填写直播平台的账号、密码、直播标题。从被录制端唤起广播请求并选定广播平台后,会显示 Broadcast UI 界面。
核心代码:
#import <ReplayKit/ReplayKit.h>@interface BroadcastViewController : UIViewController@end...- (IBAction)onCancelButtonPressed:(id)sender {[self userDidCancelSetup]; }- (IBAction)onOKButtonPressed:(id)sender {if (self.accountTextField.text.length == 0|| self.passwordTextField.text.length == 0|| self.channelIDTextField.text.length == 0) {return;}[self userDidFinishSetup]; }#pragma mark - Private// Called when the user has finished interacting with the view controller and a broadcast stream can start - (void)userDidFinishSetup {NSLog(@"userDidFinishSetup");// Broadcast url that will be returned to the application NSURL *broadcastURL = [NSURL URLWithString:@"http://broadcastURL_example/stream1"]; // Service specific broadcast data example which will be supplied to the process extension during broadcastNSDictionary *setupInfo = @{@"account" : self.accountTextField.text, @"password" : self.passwordTextField.text,@"channelID" : @(self.channelIDTextField.text.integerValue)}; // Set broadcast settingsRPBroadcastConfiguration *broadcastConfig = [[RPBroadcastConfiguration alloc] init];broadcastConfig.clipDuration = 5.0; // deliver movie clips every 5 seconds// Tell ReplayKit that the extension is finished setting up and can begin broadcasting[self.extensionContext completeRequestWithBroadcastURL:broadcastURL broadcastConfiguration:broadcastConfig setupInfo:setupInfo]; }- (void)userDidCancelSetup {// Tell ReplayKit that the extension was cancelled by the user[self.extensionContext cancelRequestWithError:[NSError errorWithDomain:@"YourAppDomain" code:-1 userInfo:nil]]; }
效果:
如果用户点击 OK
,则会回调到第二部分中的 RPBroadcastActivityViewControllerDelegate
,开始直播会调用 Broadcast Upload 扩展。
3.2 Broadcast Upload
第二部分调用 startBroadcastWithHandler
,会跑到 Broadcast Upload
,本扩展的作用是接收并处理 Broadcast UI 传过来的用户信息,以及处理 RPBroadcastController 传过来的音视频流数据,如编码、上传。
核心代码:
#import <ReplayKit/ReplayKit.h>@interface SampleHandler : RPBroadcastSampleHandler@end...// To handle samples with a subclass of RPBroadcastSampleHandler set the following in the extension's Info.plist file:
// - RPBroadcastProcessMode should be set to RPBroadcastProcessModeSampleBuffer
// - NSExtensionPrincipalClass should be set to this class@implementation SampleHandler- (void)broadcastStartedWithSetupInfo:(NSDictionary<NSString *,NSObject *> *)setupInfo {// User has requested to start the broadcast. Setup info from the UI extension will be supplied.NSLog(@"broadcastStartedWithSetupInfo: %@", setupInfo);[[ReplayKitUploader sharedObject] setupWithInfo:setupInfo];
}- (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 {[[ReplayKitUploader sharedObject] handleSampleBuffer:sampleBuffer withType:sampleBufferType];
}@end
3.3 注意事项
ReplayKitUploader
是自定义的一个类,使用了单例模式,负责广播服务的登录、编码、上传功能。使用单例,而不是直接在 SampleHandler 里面处理,是因为 SampleHandler 并不是 Broadcast Upload Extension 里的唯一一个实例,实际上,Upload Extension 会不断地创建很多个 SampleHandler 来处理 CMSampleBufferRef,而我们为了保存一些内部状态,必须使用一个固定的类实例来实现。
4. App 与 Extension 的代码共用
iOS 10 新增了很多种 Extension,包括本文提到的两种 Broadcast Extension。主 App 与 Extension 属于不同的两个进程,代码逻辑也是分离的,而实际情况中,主 App 与 Extension 往往会包含相同的逻辑,需要共用代码。
主 App 与 Extension 属于两个不同的 target,共用代码,有以下几种方式:
- 一份代码创建两个副本,分别加到 App 和 Extension 两个 target 中。这种方法简单粗暴而有效,只是,如果需要改动逻辑,则需要改两份代码,想象一下,假如这种改动很频繁,世界上又有几个程序员能受得了?
- 抽离公共代码,放到独立的 framework,然后两个 target 都依赖该 framework,这是标准而方便的做法。
- 使用 CocoaPods,只需要在 Podfile 中分别写两个 target 所依赖的 pod 即可,最简洁。
5. 结论
在 iOS 环境中,想要共享设备屏幕,无论是录播还是直播,都注定了没有直接方便的方案。ReplayKit Live 是目前最标准的做法,只是,使用 ReplayKit 有两个前提,应用本身支持 ReplayKit,直播平台支持 Broadcast Extension。这两个前提,后者比较容易实现,而前者,就需要靠各个应用开发商的自觉了。
原文地址:http://blog.lessfun.com/blog/2016/09/21/ios-10-replaykit-live-and-broadcast-extension/
ios 10 开发-录屏直播 ReplayKit Live 与 Broadcast UI/Upload Extension相关推荐
- iOS录屏直播(二)Broadcast Upload Extension和Broadcast Setup UI Extension
Morris_2019.06.13 上一篇总结了ReplayKit相关的知识点,实现了应用内的录屏功能,同时涉及到了很少一部分Broadcast Upload Extension和Broadcast ...
- iOS rtmp 摄像头/录屏直播以及观看
之前讲过如何在centos上使用nginx搭建rtmp服务器(链接),本文介绍一下iOS 端如何通过rtmp录屏直播以及观看,完整的工程代码地址(https://github.com/zxm006/R ...
- ios 自带录屏框架replayKit的使用
前几个月第一次做关于ios录屏的功能,在网上看到有关于replaykit的介绍,总结之后集成到项目中,初步达到了项目要求的录屏的功能,但是在后续的测试发现,有录屏出现黑屏的情况,也有不能保存到系统系统 ...
- iOS录屏直播(三)AppGroup
Morris_2019.06.14 AppGroup是什么 App Groups Entitlement AppGroup是一个App组,里面可以有若干个App,AppGroup组是个虚无的存在,若干 ...
- iOS录屏直播(一)初识ReplayKit
Morris_2019.05.08 本篇主要功能: 认识ReplayKit框架 RPScreenRecorder实现在应用内录屏功能 RPPreviewViewController查看录屏内容 RPB ...
- iOS录屏直播(四)主App和宿主App数据共享,通信功能实现
文章目录 CFNotificationCenterGetDarwinNotifyCenter 发送通知 接收通知 注意事项 遗留问题 补充 Morris_ 2019.06.17 上一篇总结了一下App ...
- 手游录屏直播技术详解 | 直播 SDK 性能优化实践
直播无疑是 2016 年的大热话题,七牛云在 6 月底发布了实时流网络 LiveNet 和直播云解决方案后,我们用<直播技术详解>系列文章系统地介绍了直播各个环节的关键技术,帮助视频直播创 ...
- android实现录像功能吗,Android实现录屏直播(一)ScreenRecorder的简单分析
应项目需求瞄准了Bilibili的录屏直播功能,基本就仿着做一个吧.研究后发现Bilibili是使用的MediaProjection 与 VirtualDisplay结合实现的,需要 Android ...
- c语言安卓录屏,手写 Android 录屏直播
简介 观看手游直播时,我们观众端看到的是选手的屏幕上的内容,这是如何实现的呢?这篇博客将手写一个录屏直播 Demo,实现类似手游直播的效果 获取屏幕数据很简单,Android 系统有提供对应的服务,难 ...
最新文章
- 【OpenCV】将单通道的Mat对象转换为三通道的Mat
- 贪心:Jump Game 跳跃游戏
- javascript worker 多线程 简单示例
- 记录一次最新版MySQL-server-5.6.20-1.el6.x86_64.rpm的安装
- ASP.NET获得客户端浏览器语言设置(Get the Language setting of browser by ASP.NET)
- Ransomware CryptXXX Analysis
- Dom4j使用Xpath语法读取xml节点
- [QTP] 描述性编程
- 删除同样元素(线性表)
- 消息分流器-HANDLE_MSG
- ORACLE WebLogic Server 安装部署
- MySQL数据库无法启动的简单排错
- 智能客服FAQ问答任务的技术选型探讨
- 数据结构--哈希(Hash)和代码实现(详解)
- lisp princ详解_LISP – 输入和输出
- 2020程序员工资排行:腾讯阿里全部落榜,字节跳动高薪实锤
- 什么是Photoshop中的图层和蒙版?
- 十张数据图回顾雾霾,北京污染从南向北加深趋势明显
- Win10家庭中文版( 连接远程桌面要求的函数不受支持、这可能是由于 CredSSP 加密 Oracle 修正 )
- MATLAB绘制B样条曲线