ios 自带录屏框架replayKit的使用
程序员文章站
2022-07-05 23:07:41
前几个月第一次做关于ios录屏的功能,在网上看到有关于replaykit的介绍,总结之后集成到项目中,初步达到了项目要求的录屏的功能,但是在后续的测试发现,有录屏出现黑屏的情况,也有不能保存到系统...
前几个月第一次做关于ios录屏的功能,在网上看到有关于replaykit的介绍,总结之后集成到项目中,初步达到了项目要求的录屏的功能,但是在后续的测试发现,有录屏出现黑屏的情况,也有不能保存到系统相册的情况,后来及时改改逻辑,调用顺序就解决,现在是没什么问题了,但是感觉也不是太稳定,还没正式的使用,还不能确定不会出现问题,现在,把代码贴出来,欢迎指导指正
replaykit是ios9后出的,所以replaykit支持ios9+录屏,
//开始录屏 - (void)startrecoder { //将开启录屏功能的代码放在主线程执行 dispatch_async(dispatch_get_main_queue(), ^{ if ([[rpscreenrecorder sharedrecorder] isavailable] && [self issystemversionok]) { //判断硬件和ios版本是否支持录屏 nslog(@"支持replaykit录制"); //这是录屏的类 rpscreenrecorder* recorder = rpscreenrecorder.sharedrecorder; recorder.delegate = self; __weak typeof (self)weakself = self; mbprogresshud *hud; hud = [mbprogresshud showhudaddedto:self.view animated:yes]; hud.maskview.hidden = no; hud.labeltext = nslocalizedstring(@"正在启动录屏...", @"hud loading title"); [self delay:8 execute:^{ if (hud.hidden == no) { hud.hidden = yes; [self showalert:@"录屏启动失败" andmessage:@"请重试"]; } }]; //在此可以设置是否允许麦克风(传yes即是使用麦克风,传no则不是用麦克风) recorder.microphoneenabled = yes; recorder.cameraenabled = yes; //开起录屏功能 [recorder startrecordingwithhandler:^(nserror * _nullable error) { //在此可以设置是否允许麦克风(传yes即是使用麦克风,传no则不是用麦克风)(ios 后被废弃了 下面的这个开始录屏) // [recorder startrecordingwithmicrophoneenabled:yes handler:^(nserror * _nullable error) { recorder.microphoneenabled = yes; if (error) { hud.hidden = yes; isdecoderscreen = 0; mbprogresshud *hud = [mbprogresshud showhudaddedto:self.view animated:yes]; hud.mode = mbprogresshudmodetext; hud.labeltext = @"录屏启动失败!\n请重试"; hud.margin = 10.f; hud.yoffset = 150.f; hud.removefromsuperviewonhide = yes; [hud hide:yes afterdelay:4]; nslog(@"========%@",error.description); _decoderbutton.selected = no; } else { if (recorder.recording) { _decoderbutton.selected = yes; //记录是否开始录屏 系统也有一个再带的检测是否在录屏的方法 (@property (nonatomic, readonly, getter=isrecording) bool recording;) isdecoderscreen = 1; hud.hidden = yes; } } }]; } else { [self showalert:@"设备不支持录制" andmessage:@"升级ios系统"]; return; } }); }
//结束录屏 - (void)stopdecoder { __weak typeof (self)weakself = self; [[rpscreenrecorder sharedrecorder] stoprecordingwithhandler:^(rppreviewviewcontroller *previewviewcontroller, nserror * error){ _rppreview = previewviewcontroller; if (error) { nslog(@"这里关闭有误%@",error.description); } else { [_rppreview setpreviewcontrollerdelegate:self]; _decoderbutton.selected = no; isdecoderscreen = 0; //在结束录屏时显示预览画面 [weakself showvideopreviewcontroller:_rppreview withanimation:yes]; } }]; }
//显示视频预览页面,animation=是否要动画显示 - (void)showvideopreviewcontroller:(rppreviewviewcontroller *)previewcontroller withanimation:(bool)animation { __weak typeof (self) weakself = self; //ui需要放到主线程 dispatch_async(dispatch_get_main_queue(), ^{ cgrect rect = previewcontroller.view.frame; if (animation) { rect.origin.x += rect.size.width; previewcontroller.view.frame = rect; rect.origin.x -= rect.size.width; [uiview animatewithduration:0.3 animations:^(){ previewcontroller.view.frame = rect; } completion:^(bool finished){ }]; } else { previewcontroller.view.frame = rect; } [weakself.view addsubview:previewcontroller.view]; [weakself addchildviewcontroller:previewcontroller]; }); }
//关闭视频预览页面,animation=是否要动画显示 - (void)hidevideopreviewcontroller:(rppreviewviewcontroller *)previewcontroller withanimation:(bool)animation { //ui需要放到主线程 dispatch_async(dispatch_get_main_queue(), ^{ cgrect rect = previewcontroller.view.frame; if (animation) { rect.origin.x += rect.size.width; [uiview animatewithduration:0.3 animations:^(){ previewcontroller.view.frame = rect; } completion:^(bool finished){ //移除页面 [previewcontroller.view removefromsuperview]; [previewcontroller removefromparentviewcontroller]; }]; } else { //移除页面 [previewcontroller.view removefromsuperview]; [previewcontroller removefromparentviewcontroller]; } }); }
#pragma mark -rppreviewviewcontrollerdelegate //关闭的回调 - (void)previewcontrollerdidfinish:(rppreviewviewcontroller *)previewcontroller { if (issave == 1) { //这个地方我添加了一个延时,我发现这样保存不到系统相册的情况好多了 dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(4 * nsec_per_sec)), dispatch_get_main_queue(), ^{ [self hidevideopreviewcontroller:_rppreview withanimation:yes]; }); issave = 0; }else { dispatch_async(dispatch_get_main_queue(), ^{ uialertaction *queding = [uialertaction actionwithtitle:@"确定" style:uialertactionstyledefault handler:^(uialertaction * _nonnull action) { [self hidevideopreviewcontroller:_rppreview withanimation:yes]; // dispatch_async(dispatch_get_main_queue(), ^{ // [weakself.rppreview dismissviewcontrolleranimated:yes completion:nil]; // }); issave = 0; }]; uialertaction *actioncancel = [uialertaction actionwithtitle:@"取消" style:uialertactionstylecancel handler:nil]; uialertcontroller *alert = [uialertcontroller alertcontrollerwithtitle:@"提示" message:@"录制未保存\n确定要取消吗" preferredstyle:uialertcontrollerstylealert]; [alert addaction:actioncancel]; [alert addaction:queding]; [self presentviewcontroller:alert animated:no completion:nil]; }); } } //选择了某些功能的回调(如分享和保存) - (void)previewcontroller:(rppreviewviewcontroller *)previewcontroller didfinishwithactivitytypes:(nsset *)activitytypes { __weak typeof (self)weakself = self; if ([activitytypes containsobject:@"com.apple.uikit.activity.savetocameraroll"]) { issave = 1; nslog(@"***************************"); //这个地方我延时执行,等预览画面移除后再显示提示框 dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(4 * nsec_per_sec)), dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{ [weakself showalert:@"保存成功" andmessage:@"已经保存到系统相册"]; }); }); } if ([activitytypes containsobject:@"com.apple.uikit.activity.copytopasteboard"]) { dispatch_async(dispatch_get_main_queue(), ^{ [weakself showalert:@"复制成功" andmessage:@"已经复制到粘贴板"]; }); } }
#pragma mark ====rpscreendelegate=== - (void)screenrecorderdidchangeavailability:(rpscreenrecorder *)screenrecorder { // [screenrecorder addobserver:self forkeypath:@"recording" options:nskeyvalueobservingoptionnew context:nil]; // [screenrecorder setvalue:@"1" forkey:@"recording"]; nslog(@" delegate ======%@",screenrecorder); } - (void)screenrecorder:(rpscreenrecorder *)screenrecorder didstoprecordingwitherror:(nserror *)error previewviewcontroller:(nullable rppreviewviewcontroller *)previewviewcontroller { [_rppreview setpreviewcontrollerdelegate:self]; _decoderbutton.selected = no; isdecoderscreen = 0; [self showvideopreviewcontroller:_rppreview withanimation:yes]; } - (void)observevalueforkeypath:(nsstring *)keypath ofobject:(id)object change:(nsdictionary *)change context:(void *)context { if ([keypath isequaltostring:@"recording"]) { nslog(@"keypath === %@",object); if ([change valueforkey:@"recording"] == 0) { nslog(@"可以录制"); }else { nslog(@"++++++++++++不可以"); } } }
//显示弹框提示 - (void)showalert:(nsstring *)title andmessage:(nsstring *)message { if (!title) { title = @""; } if (!message) { message = @""; } uialertaction *actioncancel = [uialertaction actionwithtitle:@"好的" style:uialertactionstylecancel handler:nil]; uialertcontroller *alert = [uialertcontroller alertcontrollerwithtitle:title message:message preferredstyle:uialertcontrollerstylealert]; [alert addaction:actioncancel]; [self presentviewcontroller:alert animated:no completion:nil]; }
//判断对应系统版本是否支持replaykit - (bool)issystemversionok { if ([[uidevice currentdevice].systemversion floatvalue] < 9.0) { return no; } else { return yes; } }
上一篇: ORACLE实现字段自增