一、视频的开始采集和停止采集
// 1.创建捕捉会话
let session = AVCaptureSession()```
- 2.给捕捉会话设置输入源(摄像头)
![获取device的几种方法.png](https://img.haomeiwen.com/i1429890/7f1a13d562b94904.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
// 2.获取捕捉会话的输入源
// 2.1获取摄像头设备
guard let devices = AVCaptureDevice.devices(withMdeiaType: AvMediaTypeVideo) as? [AVCaptureDevice] else { return }
guard let device = devices.filter({ $0.position == .fornt }).first else { return }
// 2.2通过device创建AVCaptureInput对象
guard let videoInput = AVCaptureDeviceInput(device: device) else { return }
// 2.3将input添加到会话中
session.addInput(videoInput)```
// 3.给捕捉会话设置输出源
let videoOutput = AVCaptureVideoDataOutput()
videoOutput.setSamoleBufferDelegate(self,queue: DdispatchQueue.global())
session.addOutput(videoOutput)```
- 4.给用户设置一个可以看到的图层(可选)
// 4.给用户看到一个预览图层
let preViewLayer = AVCaptureVideoPreviewLayer()
preViewLayer.frame = view.bounds
view.layer.addSubLayer(preViewLayer)```
// 5.开始采集
session.startRunning()```
***
- ###停止采集步骤
- 1.让session停止采集
- 2.移除预览图层
![](https://img.haomeiwen.com/i1429890/cb496322af8b5670.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- ###设置代理,执行代理方法
![](https://img.haomeiwen.com/i1429890/c2e79b26c692d1ad.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- ###iOS 10在info.plist设置摄像头权限
![Paste_Image.png](https://img.haomeiwen.com/i1429890/5b39ea6ea3a0541f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 参考代码(设置视频采集)
- ```
func setVideoCapture()
{
// 1.创建捕捉会话
let session = AVCaptureSession()
// 2.获取捕捉会话的输入源
// 2.1获取摄像头设备
guard let devices = AVCaptureDevice.devices(withMdeiaType: AvMediaTypeVideo) as? [AVCaptureDevice] else { return }
guard let device = devices.filter({ $0.position == .fornt }).first else { return }
// 2.2通过device创建AVCaptureInput对象
guard let videoInput = AVCaptureDeviceInput(device: device) else { return }
// 2.3将input添加到会话中
session.addInput(videoInput)
// 3.给捕捉会话设置输出源
let videoOutput = AVCaptureVideoDataOutput()
videoOutput.setSamoleBufferDelegate(self,queue: DdispatchQueue.global())
session.addOutput(videoOutput)
// 4.给用户看到一个预览图层
let preViewLayer = AVCaptureVideoPreviewLayer()
preViewLayer.frame = view.bounds
view.layer.addSubLayer(preViewLayer)
// 5.开始采集
session.startRunning()
} ```
##注意: 会话session和预览图层使用强引用,懒加载属性即可(截图展示)
![完整代码展示.png](https://img.haomeiwen.com/i1429890/567e605f6a2da472.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
***
***
***
#二、音频的开始采集和停止采集
步骤和视频一样,这里贴出不同之处的核心代码
![音频的开始采集](https://img.haomeiwen.com/i1429890/3466060f97306469.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- ###iOS 10在info.plist设置摄像头权限
![Paste_Image.png](https://img.haomeiwen.com/i1429890/5b39ea6ea3a0541f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
##注意: 由于音视频的代理方法相同,我们根据connection区分音视频
![](https://img.haomeiwen.com/i1429890/6e9e20fc010aac59.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![connection.png](https://img.haomeiwen.com/i1429890/475f592d5dda5969.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://img.haomeiwen.com/i1429890/a72edccba3ef2641.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
**至此,完成简单的音视频采集**
***
***
***
- ###切换摄像头
![切换摄像头.png](https://img.haomeiwen.com/i1429890/a8d1585ff272b3a0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- ###保存视频
- 添加写入文件的output(在开始采集视频的时候开始写入)
![](https://img.haomeiwen.com/i1429890/1758391661189181.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 代理,可监听什么时候开始写入(在停止采集视频的时候停止文件写入)
![写入.png](https://img.haomeiwen.com/i1429890/60e3dc1a8d053cb5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![代理](https://img.haomeiwen.com/i1429890/06646f76e7c75aae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![停止录制](https://img.haomeiwen.com/i1429890/80c7ffcf2e2d3ac3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
####后续会放上完整代码补充、
***
***
***
- #github
| 项目 | 简介 |
| : | : |
| [MGDS_Swif](https://github.com/LYM-mg/MGDS_Swift) | 逗视视频直播 |
| [MGMiaoBo](https://github.com/LYM-mg/MGMiaoBo) | 喵播视频直播 |
| [MGDYZB](https://github.com/LYM-mg/MGDYZB) | 斗鱼视频直播 |
| [MGDemo](https://github.com/LYM-mg/MGDemo) | n多小功能合集 |
| [MGBaisi](https://github.com/LYM-mg/MGBaisi) | 高度仿写百思 |
| [MGSinaWeibo](https://github.com/LYM-mg/MGSinaWeibo) | 高度仿写Sina |
| [MGLoveFreshBeen](https://github.com/LYM-mg/MGLoveFreshBeen) | 一款电商App |
| [MGWeChat](https://github.com/LYM-mg/MGWeChat) | 小部分实现微信功能 |
| [MGTrasitionPractice](https://github.com/LYM-mg/MGTrasitionPractice) | 自定义转场练习 |
| [DBFMDemo](https://github.com/LYM-mg/DBFMDemo) | 豆瓣电台 |
| [MGPlayer](https://github.com/LYM-mg/MGPlayer) | 一个播放视频的Demo |
| [MGCollectionView](https://github.com/LYM-mg/MGCollectionView) | 环形图片排布以及花瓣形排布 |
| [MGPuBuLiuDemo](https://github.com/LYM-mg/MGPuBuLiuDemo) | 瀑布流--商品展 |
| [MGSlideViewDemo](https://github.com/LYM-mg/MGSlideViewDemo) | 一个简单点的侧滑效果,仿QQ侧滑 |
| [MyResume](https://github.com/LYM-mg/MyResume) | 一个展示自己个人简历的Demo |
| [GoodBookDemo](https://github.com/LYM-mg/GoodBookDemo) | 好书 |
- #[1、直播喵播MGMiaoBo下载](https://github.com/LYM-mg/MGMiaoBo)
- #[2、逗视:逗你玩的直播App,可下载试玩](https://github.com/LYM-mg/MGDS_Swift)
- >#看下效果
![逗视介绍1.gif](https://img.haomeiwen.com/i1429890/ecd25e08d367c32e.gif?imageMogr2/auto-orient/strip)
![逗视介绍2.gif](https://img.haomeiwen.com/i1429890/91b427263bc09abd.gif?imageMogr2/auto-orient/strip)
***
网友评论