1. 视频录制方式一
#import <MobileCoreServices/MobileCoreServices.h>
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//使用UIImagePickerController
/*
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
*/
UIImagePickerController *pickerC = [UIImagePickerController new];
//源类型
pickerC.sourceType = UIImagePickerControllerSourceTypeCamera;
//媒体类型 --> 默认只有图像
//kUTTypeImage: 系统封装的一个字符串
//@"public.movie" == kUTTypeMovie;
pickerC.mediaTypes = @[ (NSString *)kUTTypeMovie];
[self presentViewController:pickerC animated:YES completion:nil];
}
2. 视频录制方式二
/*
1. 输入设备 默认后置摄像头&麦克风
2. 输出数据 视频Movie数据
3. 会话 关联输入和输出
4. Layer 用于显示摄像头拍摄的内容
*/
@property (nonatomic, strong) AVCaptureDeviceInput *videoInput;
@property (nonatomic, strong) AVCaptureDeviceInput *audioInput;
@property (nonatomic, strong) AVCaptureMovieFileOutput *output;
@property (nonatomic, strong) AVCaptureSession *session;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)statClick:(id)sender {
// 1. 输入设备 默认后置摄像头&麦克风
//AVMediaTypeVideo默认是后置摄像头, AVMediaTypeAudio默认是麦克风
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
self.audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
//2. 输出数据 视频Movie数据
self.output = [AVCaptureMovieFileOutput new];
//3. 会话 关联输入和输出
self.session = [AVCaptureSession new];
if ([self.session canAddInput:self.videoInput]) {
[self.session addInput:self.videoInput];
}
if ([self.session canAddInput:self.audioInput]) {
[self.session addInput:self.audioInput];
}
if ([self.session canAddOutput:self.output]) {
[self.session addOutput:self.output];
}
//4. Layer 用于显示摄像头拍摄的内容
self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
self.previewLayer.frame = CGRectMake(0, 0, 375, 500);
[self.view.layer addSublayer:self.previewLayer];
//5. 开启会话
[self.session startRunning];
//6. 开始录制
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"jiritian.mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
//OutputFileURL: 存储录制视频的路径
[self.output startRecordingToOutputFileURL:url recordingDelegate:self];
}
- (IBAction)stopClick:(id)sender {
//1. 停止录制
[self.output stopRecording];
//2. 停止session
[self.session stopRunning];
//3. 移除layer
[self.previewLayer removeFromSuperlayer];
}
#pragma mark - AVCaptureFileOutputRecordingDelegate
#pragma mark 完成录制 必须实现的方法
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error {
if (error) {
NSLog(@"error: %@", error);
} else {
NSLog(@"录制完成");
//播放视频
AVPlayerViewController *pvc = [AVPlayerViewController new];
pvc.player = [AVPlayer playerWithURL:outputFileURL];
[pvc.player play];
[self presentViewController:pvc animated:YES completion:nil];
//保存视频
//1. 创建资源库对象
ALAssetsLibrary *al = [ALAssetsLibrary new];
//2. 直接写入视频到相册中
[al writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"写入失败: %@", error);
} else {
NSLog(@"保存成功");
}
}];
}
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections {
NSLog(@"开始录制");
}