通过拍摄照片Demo的学习,我们学会了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地址: https://github.com/WorthyZhang/WZMediaDemo

AVFoundation学习Demo--拍摄视频相关推荐

  1. AVFoundation学习记录

    学习文档 (强烈推荐)移动端音视频从零到上手 About AVFoundation 学习教程 AVFoundation框架解析 具体功能 拍摄 镜头变焦 变焦的方式主要有两种实现: 通过修改AVCap ...

  2. 高德地图开发学习Demo

    高德地图开发学习Demo 提供者:sannas 开源项目地址: https://github.com/851151582/NaviLocXf.git Demo实现地图定位.搜索及导航功能,通过关键字可 ...

  3. AVFoundation学习之视频录制

    title: AVFoundation学习之视频录制 date: 2019-08-22 16:28:50 tags: 一.AVAsset理解 *前一篇完整视频播放里面的AVPlayerItem初始化可 ...

  4. 微信小程序学习DEMO,微信小程序初学者可以参考

    这是一套微信初学者学习DEMO源码,有需要的同学可到下载频道下载~ 这个微信小程序包含了以下几个功能,非常适合微信小程序开发小白,公司实习生,毕业设计参考. 1.朋友圈功能,可以发布朋友圈,九官格图片 ...

  5. 电商网站学习demo(一)--首页

    电商网站学习demo 网页效果 HTML代码 <!DOCTYPE html> <html><head><meta charset="utf-8&qu ...

  6. AVFoundation学习之视频播放

    title: AVFoundation学习之视频播放 date: 2019-08-22 11:26:37 tags: 一.CMTime理解 *AV Foundation中使用CMTime数据结构记录时 ...

  7. AVFoundation 学习资源列表

    天才第一步: link:https://developer.apple.com/reference/avfoundation?language=objc#overview link:https://d ...

  8. NickLee的学习demo(父子列表清单)

    前面我有给NickLee的一个控件地址,不过在线学习没有demo我们很多时候很难下手,我做了个简单的demo,是关于父子列表清单,我们在很多时候做erp会遇见bom清单,也就是一个订单对应哪些产品,而 ...

  9. ES学习demo大全

    为了便于以后使用ES方便,自己整理了ES使用的demo,如下: package mainimport ("context""encoding/json"&quo ...

最新文章

  1. 慈溪计算机编程培训,慈溪Python编程培训
  2. python2.7除法_对python中的float除法和整除法的实例详解
  3. 基于vue-cli,做个nuxt脚手架~
  4. java常见类关系(UML建模)
  5. 【Android 逆向】Android 进程注入工具开发 ( 注入代码分析 | 远程调用 目标进程中 libc.so 动态库中的 mmap 函数 三 | 等待远程函数执行完毕 | 寄存器获取返回值 )
  6. MYSQL韩文显示正常一法
  7. C# 构造函数中调用虚函数
  8. 在.c文件中调用cuda函数
  9. CF :K 一个含n条边的带权无向连通图,q次查询,每次查询两点间的最短距离。...
  10. Python开发环境Wing IDE 5.0测试第八版发布
  11. memcache服务应用实践
  12. 95-22-010-停止-优雅停机
  13. Linux命令解释之passwd
  14. AOP 你想干什么 IOC 你服务什么
  15. centos 6.5 httpd 服务
  16. import matplotlib.pyplot as plt
  17. 【MATLAB】MATLAB基本运算
  18. matlab gui的callback,matlab GUI callback 函数实现
  19. 实时应用监控平台CAT
  20. activemq 简介 配置

热门文章

  1. element引入的组件大小高度不对_试水 elementplus ui 组件库
  2. CSS+HTML大白
  3. Miniconda3的环境配置
  4. python2.7环境下“No module named numpy”的解决办法
  5. Vivado中Debug的用法总结
  6. 【 FPGA 】UltraFast设计方法学:如何管理IP约束
  7. 【刷算法】LeetCode.278-第一个错误的版本
  8. 计算机科学中抽象的好处与问题—伪共享等实例分析
  9. Atom不能补全原生JS的一些DOM函数
  10. iOS开发使用Unwind Segue进行返回