使用原生AVCapture系列
来源:互联网 发布:不懂英文能学单片机 编辑:程序博客网 时间:2024/05/12 23:03
概述:
- 可用于音频、二维码、拍照、录制视频 (均可自定义界面)
常见的输出信号:
- AVCaptureAudioDataOutput 音频输出
- AVCaptureFileOutput 文本输出
- AVCaptureMetadataOutput 二维码 条形码…
- AVCaptureStillImageOutput 拍照
- AVCaptureMovieFileOutput 录制视频(不能实现暂停录制和定义视频文件类型)
- AVCaptureVideoDataOutput + AVCaptureAudioDataOutput 录制视频的灵活性更强(能实现暂停录制和定义视频文件类型)
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]; } }}
1 0
- 使用原生AVCapture系列
- AVCapture --- 扫描
- nodejs系列之原生系列
- 【ios】AVCapture的理解
- ios - AVCapture理解
- AVCapture实现视频采集
- iOS AVcapture 自定义相机
- AVCapture编程理解
- iOS 自定义相机AVCapture
- java系列之 原生数据类型
- Java系列之原生数据类型
- 【java系列之原生数据类型】
- 原生DOM系列-Element对象
- iOS 10 Auto Layout界面自动布局系列3-使用原生NSLayoutConstraint添加布局约束
- 图片轮播系列1-旋转木马(使用JS原生数组方式实现)
- springMVC笔记系列(12)——使用Servlet原生API的类型参数
- Hibernate 使用原生SQL
- 使用原生的Base64
- 89.android获取联系人,并回传联系人数据(设置安全号码)
- 《深入浅出Node.js》读书笔记+个人思考(一):模块机制
- Atitit 关于建立知识库体系的方案
- 90.sim卡状态监控
- 计负均正
- 使用原生AVCapture系列
- Atititv2需求文档模板大纲目录 attilax总结
- 我的第一篇博客
- atitit 需求条目的自动化生成原型html h5界面ui与解决方案
- 51nod-1391-01串(O(n)模拟)
- HTTP协议详解(真的很经典)
- 数据结构之循环队列(顺序表存储)——C++模板类实现
- ubuntu下安装svn
- IP地址详解