利用replayKit 简单录制

只做出了简单的录制效果,还有很多需要优化,
遇到的最大问题是最后的视频界面是英文的,无法改成中文,费解

- (void)createUI
{
    
    for (int i=0; i<2; i++) {
        
        UIButton * tempButton = [[UIButton alloc]initWithFrame:CGRectMake((Device_Width - FIT_WIDTH(200))/2 + FIT_WIDTH(100)*i, FIT_HEIGHT(100), FIT_WIDTH(100), FIT_HEIGHT(40))];
        tempButton.backgroundColor = [UIColor lightGrayColor];
        tempButton.tag = 100+i;
        [tempButton setTitle:@"start" forState:UIControlStateNormal];
        if (i == 1) {
            [tempButton setTitle:@"stop" forState:UIControlStateNormal];
        }
        [tempButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:tempButton];
       
    }
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"stopButton"]) {
        
        [self setButtonColorWithTag:100 enable:YES];
        [self setButtonColorWithTag:101 enable:NO];
    }
    else{
        [self setButtonColorWithTag:100 enable:NO];
        [self setButtonColorWithTag:101 enable:YES];
    }
    
}
- (void)setButtonColorWithTag:(int)tag enable:(BOOL)enable
{
    UIButton * button = [self.view viewWithTag:tag];
    NSString * buttonName = (tag == 100 ? @"startButton" : @"stopButton");
    
    [[NSUserDefaults standardUserDefaults] setBool:enable forKey:buttonName];
    
    if (enable) {
        // 可点击
        button.backgroundColor = [UIColor whiteColor];
        button.layer.borderColor = Color_248.CGColor;
        button.layer.borderWidth = 1;
        [button setTitleColor:Color_248 forState:UIControlStateNormal];
        button.enabled = YES;
    }
    else{
        // 不可点击
        button.backgroundColor = [UIColor whiteColor];
        button.layer.borderColor = Color_242.CGColor;
        button.layer.borderWidth = 1;
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        button.enabled = NO;
    }
    
    
}
- (void)buttonClick:(UIButton *)button
{
    
    if (button.tag ==100) {
        //start
        
        [self setButtonColorWithTag:100 enable:NO];
        [self setButtonColorWithTag:101 enable:YES];
        
        if ([RPScreenRecorder sharedRecorder].available) {
           
            [[RPScreenRecorder sharedRecorder] startRecordingWithHandler:^(NSError * _Nullable error) {
                [RPScreenRecorder sharedRecorder].microphoneEnabled = YES;//麦克风
                if (error) {
                    [self showAlert:@"错误" andMessage:error.description];
                }
               
            }];
        }
        else{
            [self showAlert:@"提示" andMessage:@"录制不可用"];
        }
        
    }
    else{
        //stop
        
        [self setButtonColorWithTag:100 enable:YES];
        [self setButtonColorWithTag:101 enable:NO];
       
        [[RPScreenRecorder sharedRecorder] stopRecordingWithHandler:^(RPPreviewViewController * _Nullable previewViewController, NSError * _Nullable error) {
            if (error) {
               [self showAlert:@"错误" andMessage:error.description];
            }
            else{
                 //显示录制到的视频的预览页
                previewViewController.previewControllerDelegate = self;
                [self presentViewController:previewViewController animated:YES completion:nil];
                
            }
        }];
    }
    
    
}

#pragma mark - 视频预览页面 回调
//关闭的回调
- (void)previewControllerDidFinish:(RPPreviewViewController *)previewController {

    [previewController dismissViewControllerAnimated:YES completion:nil];

}
//选择了某些功能的回调(如分享和保存)
- (void)previewController:(RPPreviewViewController *)previewController didFinishWithActivityTypes:(NSSet <NSString *> *)activityTypes {
    
    __weak GSRecordingViewController *weakSelf = self;
    if ([activityTypes containsObject:@"com.apple.UIKit.activity.SaveToCameraRoll"]) {
        
        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:@"已经复制到粘贴板"];
        });
    }
}
//显示弹框提示
- (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];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值