利用replayKit 简单录制

xiaoxiao2021-02-28  89

只做出了简单的录制效果,还有很多需要优化, 遇到的最大问题是最后的视频界面是英文的,无法改成中文,费解 - (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]; }
转载请注明原文地址: https://www.6miu.com/read-29268.html

最新回复(0)