最近在弄屏幕共享事宜亚享,看了很多文章了解了從iOS9一直到iOS12 蘋果在直播錄像方面做的改變啄枕。
如果你對(duì)直播實(shí)現(xiàn)的演變感興趣請(qǐng)點(diǎn)擊此處:傳送門
現(xiàn)在說(shuō)一下iOS12 用Replaykit2 獲取屏幕流的過(guò)程:
屏幕流獲取
1:在項(xiàng)目中添加Targets
2:添加Broadcast Upload Extension--->Next
3:在你想要實(shí)現(xiàn)屏幕共享的界面添加調(diào)取方法
#import "ViewController.h"
#import <ReplayKit/ReplayKit.h>
#define TAG_SHARESCREEN 10086
@interface ViewController ()
@property (nonatomic, strong) RPSystemBroadcastPickerView*broadPickerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_broadPickerView = [[RPSystemBroadcastPickerView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
_broadPickerView.preferredExtension = @"此處填寫你創(chuàng)建的Broadcast Upload Extension 的Bundle id(不是SetupUI的那個(gè))";
[self.view addSubview:_broadPickerView];
}
@end
運(yùn)行點(diǎn)擊屏幕上的按鈕便調(diào)取出來(lái)了開始錄屏的界面
根據(jù)需求:可能不想要系統(tǒng)自帶的按鈕霹疫,可以做以下優(yōu)化:
#import "ViewController.h"
#import <ReplayKit/ReplayKit.h>
#define TAG_SHARESCREEN 10086
@interface ViewController ()
@property (nonatomic, strong) RPSystemBroadcastPickerView*broadPickerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_broadPickerView = [[RPSystemBroadcastPickerView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
_broadPickerView.preferredExtension = @"此處填寫你創(chuàng)建的Broadcast Upload Extension 的Bundle id(不是SetupUI的那個(gè))";
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 300, 40)];
[button setTitle:@"點(diǎn)我就好了" forState:UIControlStateNormal];
[button addTarget:self action:@selector(clickedOnStartRecordButton:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
button.tag = TAG_SHARESCREEN;
[self.view addSubview:button];
}
- (void)clickedOnStartRecordButton:(UIButton *)sender
{
if (sender.tag == TAG_SHARESCREEN)
{
for (UIView *view in _broadPickerView.subviews)
{
if ([view isKindOfClass:[UIButton class]])
{
//調(diào)起錄像方法绵疲,UIControlEventTouchUpInside的方法看其他文章用的是UIControlEventTouchDown梭姓,
//我使用時(shí)用UIControlEventTouchUpInside用好使仪搔,看個(gè)人情況決定
[(UIButton*)view sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}
}
}
@end
使用RPSystemBroadcastPickerView調(diào)取系統(tǒng)錄制頁(yè)面時(shí)忧换,如果手機(jī)系統(tǒng)為:可能會(huì)導(dǎo)致調(diào)取,本人了iOS12.1之前版本的功能使用亚茬,請(qǐng)諸位自行整理解決辦法酪耳。(此乃蘋果系統(tǒng)的坑、無(wú)法解決----如有大佬有解決方法刹缝,請(qǐng)您不吝賜教~小狼拜謝ing)
調(diào)試:
1碗暗、先運(yùn)行項(xiàng)目(test_fore)到手機(jī)上
此時(shí)操作錄屏等操作,只能斷點(diǎn)到test_fore里的代碼
2梢夯、檢測(cè)test_fore_sharing中的數(shù)據(jù):運(yùn)行test_fore_sharing 選擇上述的項(xiàng)目
此時(shí)可斷點(diǎn)到 test_fore_sharing 下的所有代碼
3讹堤、檢測(cè)test_fore_sharingSetupUI:運(yùn)行test_fore_sharingSetupUI
此時(shí)可斷點(diǎn)到 test_fore_sharingSetupUI 下的所有代碼
同步數(shù)據(jù)
把test_fore_sharing獲取的屏幕流推送給test_fore(主App),test_fore(主App)推送屏幕流給RTC
可參考文章:
網(wǎng)易云信_(tái)Socket
網(wǎng)易云信_(tái)App Groups
華為云_App Groups//已封裝好SDK
騰訊云_CFNotificationCenter
騰訊云_App Group
暫時(shí)使用的是Socket方法厨疙,等實(shí)現(xiàn)完善后會(huì)發(fā)布完善的方法
類庫(kù)的使用方法
1洲守、使用了Socket和Codec兩個(gè)文件夾中的代碼,把兩個(gè)文件夾拖入自己的主App中沾凄。
2梗醇、SampleHandler中使用時(shí),需要關(guān)聯(lián)主App加載的代碼撒蟀,方法如下:
如果您有好的方法請(qǐng)不要吝惜叙谨,留言告訴我吧~
關(guān)于調(diào)起方法遇到的問(wèn)題:我遇到的iOS12.4.4使用此方法是調(diào)用不起來(lái)的,又嘗試了UIControlEventTouchDown可以成功保屯,可是方法分界的版本號(hào)就成了問(wèn)題手负、又跟杭研融合通信iOS哥哥溝通了一下、推薦使用調(diào)起
關(guān)于數(shù)據(jù)同步的補(bǔ)充:
進(jìn)程間數(shù)據(jù)同步:CFNotificationCenter與App Groups 結(jié)合