IOS ReplayKit RPScreenRecorder 的屏幕录制功能

IOS ReplayKit RPScreenRecorder 的屏幕录制功能

开始录制有两个方法:

  • (void)startRecordingWithMicrophoneEnabled:(BOOL)microphoneEnabled handler:(nullable void(^)(NSError * _Nullable error))handler);

  • (void)startRecordingWithHandler:(nullable void(^)(NSError * _Nullable error))handler);

第一种可以录制屏幕和声音,第二种仅能录屏。调用会弹出弹出框
IOS ReplayKit RPScreenRecorder 的屏幕录制功能_第1张图片

结束录制:

  • (void)stopRecordingWithHandler:(nullable void(^)(RPPreviewViewController * _Nullable previewViewController, NSError * _Nullable error))handler;

结束录制的回调中有一个previewViewController,该参数包含录制的屏幕视频预览图,顶部的返回和保存,以及底部的分享。 一般,我们只需要 present 出这个 ViewController。然后,点击保存就会自动存入相册 。也可以点击分享,直接分享到一些第三方。

代码段:

#pragma mark - 开始/结束录制
//开始录制
-(void)startRecord{
    if ([RPScreenRecorder sharedRecorder].recording==YES) {
        NSLog(@"已经开始录制");
        return;
    }

    if ([self systemVersionOK]) {

        if ([[RPScreenRecorder sharedRecorder] isAvailable]) {
            NSLog(@"录制开始初始化");
            //            [[RPScreenRecorder sharedRecorder] startRecordingWithMicrophoneEnabled:isAbleMic handler:^(NSError *error){

            [[RPScreenRecorder sharedRecorder] startRecordingWithHandler:^(NSError *error){
                if (error) {
                    NSLog(@"开始录制error %@",error);
                }
                else{
                    NSLog(@"开始录制");
                }
            }];
        }
        else {
            NSLog(@"环境不支持ReplayKit录制");
        }

        [self startScreenRecording];
    }
    else{
        NSLog(@"系统版本需要是iOS9.0及以上才支持ReplayKit录制");
    }
}

//结束录制
-(void)stopRecord{

    NSLog(@"正在结束录制");
    [[RPScreenRecorder sharedRecorder] stopRecordingWithHandler:^(RPPreviewViewController *previewViewController, NSError *  error){
        if (error) {
            NSLog(@"结束录制error %@", error);
        }
        else {
            NSLog(@"录制完成");
            [self handleVideoPreviewController:previewViewController animation:YES];

        }
    }];
}

#pragma mark - 显示/关闭视频预览页
//显示视频预览页面
-(void)handleVideoPreviewController:(RPPreviewViewController *)previewController animation:(BOOL)animation {
    previewController.previewControllerDelegate=self;

    __weak UIViewController *rootVC=[self getRootVC];

    dispatch_async(dispatch_get_main_queue(), ^{
        CGRect rect = [UIScreen mainScreen].bounds;

        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;
            }];
        }
        else{
            previewController.view.frame=rect;
        }

        [rootVC.view addSubview:previewController.view];
        [rootVC addChildViewController:previewController];
    });

}
//关闭视频预览页面
-(void)hideVideoPreviewController:(RPPreviewViewController *)previewController animation:(BOOL)animation {
    previewController.previewControllerDelegate=nil;

    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 - 视频预览页回调
//关闭的回调
- (void)previewControllerDidFinish:(RPPreviewViewController *)previewController {
    [self hideVideoPreviewController:previewController animation:YES];
}
//选择了某些功能的回调(如分享和保存)
- (void)previewController:(RPPreviewViewController *)previewController didFinishWithActivityTypes:(NSSet <NSString *> *)activityTypes {
    if ([activityTypes containsObject:@"com.apple.UIKit.activity.SaveToCameraRoll"]) {
        NSLog(@"保存到相册成功");
    }
    if ([activityTypes containsObject:@"com.apple.UIKit.activity.CopyToPasteboard"]) {
        NSLog(@"复制成功");
    }
}

#pragma mark - 其他方法
//判断对应系统版本是否支持ReplayKit
-(BOOL)systemVersionOK{
    if ([[UIDevice currentDevice].systemVersion floatValue]<9.0) {
        return NO;
    } else {
        return YES;
    }
}
//获取rootVC
-(UIViewController *)getRootVC{
    return [UIApplication sharedApplication].delegate.window.rootViewController;
}

你可能感兴趣的:(ios)