概述: 可用于音频、二维码、拍照、录制视频 (均可自定义界面) 常见的输出信号: AVCaptureAudioDataOutput 音频输出 AVCaptureFileO
概述:
可用于音频、二维码、拍照、录制视频 (均可自定义界面)
常见的输出信号:
AVCaptureMovieFileOutput输出流实现视频录制
初始化会话层
-(void)sessionConfiguration{
//初始化一个会话
session = [[AVCaptureSession alloc] init];
[session setSessionPreset:AVCaptureSessionPresetMedium];
//创建视频设备
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//根据设备创建输入信号
deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
//添加 输出设备 movieFile
self.deviceMovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
[session beginConfiguration];
//session添加设备输入信号
if ([session canAddInput:deviceInput]) {
[session addInput:deviceInput];
}
//session添加设备输出信号
if ([session canAddOutput:self.deviceMovieFileOutput]) {
[session addOutput:self.deviceMovieFileOutput];
}
[session commitConfiguration];
}
创建预览图层
-(void)embedLayerWithView:(UIView *)view{
if (session == nil) {
return;
}
videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
//设置图层的大小
videoPreviewLayer.frame = view.bounds;
videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[view.layer addSublayer:videoPreviewLayer];
[session startRunning];
}
录制视频
-(void)takePhoto:(NSURL *)fileURL{
[self.deviceMovieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self];
}
结束录制
-(UIImageView *)finishRecord:(UIView *)view isAnewRecording:(BOOL)anewRecording{
gifImageView = [[UIImageView alloc] initWithFrame:view.bounds];
[view addSubview:gifImageView];
isAnewRecording = anewRecording; //存储是否重新录制
//停止录制(停止录制后做代理方法)
[self.deviceMovieFileOutput stopRecording];
return gifImageView;
}
拍摄视频保存路径
+(NSString *)getVideoSaveFilePath{
NSString*documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentPath stringByAppendingPathComponent:@"video.mp4"];
return filePath;
}
会话层启动和关闭
-(void)startCamera{
[session startRunning];
}
-(void)stopCamera{
[session stopRunning];
}
代理方法
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{
NSLog(@"完成录制");
NSLog(@"outputFileURL = %@",outputFileURL);
//
if (isAnewRecording) {
//
NSFileManager *manager = [NSFileManager defaultManager];
[manager removeItemAtPath:outputFileURL.absoluteString error:nil];
}
//
else{
//
AVURLAsset *avUrl = [AVURLAsset URLAssetWithURL:outputFileURL options:nil];
CMTime time = [avUrl duration];
int seconds = ceil(time.value/time.timescale);
NSLog(@"seconds = %d",seconds);
if ([self.delegate respondsToSelector:@selector(videoDuration:)]) {
[self.delegate videoDuration:seconds];
}
if ([self.delegate respondsToSelector:@selector(playerVideo:)]) {
[self.delegate playerVideo:outputFileURL.absoluteString];
}
}
}
--结束END--
本文标题: iOS使用原生AVCapture系列
本文链接: https://lsjlt.com/news/30199.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0