iOS使用原生AVCapture系列
程序员文章站
2022-04-29 21:19:12
概述:
可用于音频、二维码、拍照、录制视频 (均可自定义界面)
常见的输出信号:
avcaptureaudiodataoutput 音频输出
avc...
概述:
可用于音频、二维码、拍照、录制视频 (均可自定义界面)
常见的输出信号:
- 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]; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。