概述:
可用于音频、二维码、拍照、录制视频 (均可自定义界面)
常见的输出信号:
- AVCaptureAudioDataOutput 音频输出
- AVCaptureFileOutput 文本输出
- AVCapture dataOutput 二维码 条形码…
- 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) WithView:(UIView *)view{
if (session == nil) {
return;
}
videoPreview = [AVCaptureVideoPreview WithSession:session];
//设置图层的大小
videoPreview . = view.bounds;
videoPreview .videoGravity = AV VideoGravityResizeAspectFill;
[view. addSub :videoPreview ];
[session startRunning];
}
录制视频
-(void)takePhoto:(NSURL *)fileURL{
[self.deviceMovieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self];
}
结束录制
-(UIImageView *)finishRecord:(UIView *)view isAnewRecording:(BOOL)anewRecording{
gifImageView = [[UIImageView alloc] initWith :view.bounds];
[view addSubview:gifImageView];
isAnewRecording = anewRecording; //存储是否重新录制
//停止录制(停止录制后做代理方法)
[self.deviceMovieFileOutput stopRecording];
return gifImageView;
}
拍摄视频保存路径
+(NSString *)getVideoSaveFilePath{
NSString*documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) last ];
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(p Video:)]) {
[self.delegate p Video:outputFileURL.absoluteString];
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
上一篇 :
MXNet简介
-
这3所学校人工智能专业实力强劲,毕业生被“疯抢”,值得报考
2026-05-18栏目: 教程
-
基础c4d教程:简单的木质吊灯建模,小白也能学会
2026-05-18栏目: 教程
-
大族激光:智能装备LION系列光纤激光切割机发布仪式在湘隆重召开
2026-05-18栏目: 教程
-
实拍上汽首个“无人”仓库,本月正式运行
2026-05-18栏目: 教程
-
自动驾驶光车以外的硬件就7万 滴滴想让你不买车就能先坐上
2026-05-18栏目: 教程
