博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AVFoundation学习Demo--拍摄视频
阅读量:6936 次
发布时间:2019-06-27

本文共 3837 字,大约阅读时间需要 12 分钟。

通过的学习,我们学会了AVCaptureSession的基本使用。现在,同样使用AVCaptureSession类,只需要做微小的修改就可以完成视频拍摄。

初始化�预览画面

声明属性

首先,我们要声明拍摄所必须的属性:

@property (nonatomic, strong) AVCaptureSession *captureSession;@property (nonatomic, strong) AVCaptureDevice *videoDevice;@property (nonatomic, strong) AVCaptureDevice *audioDevice;@property (nonatomic, strong) AVCaptureDeviceInput *videoInput;@property (nonatomic, strong) AVCaptureDeviceInput *audioInput;@property (nonatomic, strong) AVCaptureMovieFileOutput *movieFileOutput;@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;

因为视频会包含音频信息,所以比拍摄图片多出了音频设备(audioDevice)和音频输入(audioInput)。而我们的输出类由AVCaptureStillImageOutput变成了AVCaptureMovieFileOutput,他们同样都是AVCaptureOutput的子类。

捕捉会话初始化
在拍摄视频前,需要初始化捕捉会话(AVCaptureSession):

- (void)setupCaptureSession {    // 1.获取视频设备    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];    for (AVCaptureDevice *device in devices) {        if (device.position == AVCaptureDevicePositionBack) {            self.videoDevice = device;            break;        }    }    // 2.获取音频设备    self.audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];    // 3.创建视频输入    NSError *error = nil;    self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:self.videoDevice error:&error];    if (error) {        return;    }    // 4.创建音频输入    self.audioInput = [AVCaptureDeviceInput deviceInputWithDevice:self.audioDevice error:&error];    if (error) {        return;    }    // 5.创建视频输出    self.movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];    // 6.建立会话    self.captureSession = [[AVCaptureSession alloc] init];    self.captureSession.sessionPreset = AVCaptureSessionPreset1280x720;    if ([self.captureSession canAddInput:self.videoInput]) {        [self.captureSession addInput:self.videoInput];    }    if ([self.captureSession canAddInput:self.audioInput]) {        [self.captureSession addInput:self.audioInput];    }    if ([self.captureSession canAddOutput:self.movieFileOutput]) {        [self.captureSession addOutput:self.movieFileOutput];    }    // 7.预览画面    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];    [self.previewView.layer addSublayer:self.previewLayer];}

看到这个方法应该很熟悉了吧,和拍摄照片的初始化话几乎没有区别。需要注意的是第6步sessionPreset设为了AVCaptureSessionPreset1280x720,也就是输出720p的视频。通过查询文档,还有很多不同分辨率可供选择。

还记得开始和结束会话的两个方法吗?�startRunning和stopRunning,真是太好记了:

- (void)startSession {    if(![self.captureSession isRunning]) {        [self.captureSession startRunning];    }}- (void)stopSession {    if([self.captureSession isRunning]) {        [self.captureSession stopRunning];    }}

最后,在viewDidLoad中调用上文的两个方法:

[self setupCaptureSession];    [self startSession];

真机运行,就可以看到拍摄预览画面了。

拍摄视频

完成了初始化工作, 拍摄视频部分,我们只需要调用startRecordingToOutputFileURL:recordingDelegate:方法就可以实现了。其中,参数url是为视频指定的输出路径。

下面我们要为录制按钮建立两个Action,touch down时开始拍摄视频,touch up时结束拍摄。
touch down时,调用startRecord方法:

- (void)startRecord {    [self.movieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:[self videoPath]] recordingDelegate:self];}

touch up时,调用stopRecord方法:

- (void)stopRecord {    if ([self.movieFileOutput isRecording]) {        [self.movieFileOutput stopRecording];    }}

我们注意到,开始拍摄时,调用了videoPath方法为视频生成一个输出地址。这个方法使用当前时间戳作为唯一的视频文件名:

- (NSString *)videoPath {    NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];    NSString *moviePath = [basePath stringByAppendingPathComponent:   [NSString stringWithFormat:@"%f.mp4",[NSDate date].timeIntervalSince1970]];    return moviePath;}

最后我们,还要实现AVCaptureFileOutputRecordingDelegate代理。拍摄完成后,系统会触发captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:方法。我们可以通过outputFileURL参数访问拍摄好的视频文件。

增强摄像功能

拍摄视频和照片的一些高级功能原理是一致的,这里我只将上个Demo中�一块代码拷贝了过来,不用做任何修改就可实现摄像头切换。其他的高级设置可以查阅文档�自行探索。

Demo地址:

转载地址:http://eibnl.baihongyu.com/

你可能感兴趣的文章
centos下安装lua环境
查看>>
centos7安装ftp服务
查看>>
SpringMVC 架构、原理
查看>>
jsp中把js变量赋给java变量,或者将java变量赋给js变量怎么做
查看>>
Spring Shiro
查看>>
递归求组合数
查看>>
小蚂蚁学习数据结构(10)——树的基本介绍
查看>>
域环境迁移
查看>>
FastDFS安装使用实战一(安装篇)
查看>>
我的友情链接
查看>>
更改DEB包内容
查看>>
我的友情链接
查看>>
ibatis动态语句中的prepend
查看>>
keepalived实现LVS的高可用以及实现web服务的高可用(主从模型、双主模型)
查看>>
linux apache
查看>>
Mysql DBA 高级运维学习之路-删除表中数据
查看>>
4.26日第14次作业,23章项目整体绩效评估,24-32章信息安全相关知识
查看>>
新一代java模板引擎典范 Beetl
查看>>
centos6.8+nginx搭建简单的https服务器
查看>>
cut,sort,wc,uniq,tee,tr,split,并且,和,或者
查看>>