博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
直播的学习与使用-----采集
阅读量:7210 次
发布时间:2019-06-29

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

// 捕获音视频

- (void)setupCaputureVideo

{

// 1.创建捕获会话,必须要强引用,否则会被释放

AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];

_captureSession = captureSession;

// 2.获取摄像头设备,默认是后置摄像头

AVCaptureDevice *videoDevice = [self getVideoDevice:AVCaptureDevicePositionFront];

// 3.获取声音设备

AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];

// 4.创建对应视频设备输入对象

AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];

_currentVideoDeviceInput = videoDeviceInput;

// 5.创建对应音频设备输入对象

AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];

// 6.添加到会话中

// 注意“最好要判断是否能添加输入,会话不能添加空的

// 6.1 添加视频

if ([captureSession canAddInput:videoDeviceInput]) {

[captureSession addInput:videoDeviceInput];

}

// 6.2 添加音频

if ([captureSession canAddInput:audioDeviceInput]) {

[captureSession addInput:audioDeviceInput];

}

// 7.获取视频数据输出设备

AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init];

// 7.1 设置代理,捕获视频样品数据

// 注意:队列必须是串行队列,才能获取到数据,而且不能为空

dispatch_queue_t videoQueue = dispatch_queue_create("Video Capture Queue", DISPATCH_QUEUE_SERIAL);

[videoOutput setSampleBufferDelegate:self queue:videoQueue];

if ([captureSession canAddOutput:videoOutput]) {

[captureSession addOutput:videoOutput];

}

// 8.获取音频数据输出设备

AVCaptureAudioDataOutput *audioOutput = [[AVCaptureAudioDataOutput alloc] init];

// 8.2 设置代理,捕获视频样品数据

// 注意:队列必须是串行队列,才能获取到数据,而且不能为空

dispatch_queue_t audioQueue = dispatch_queue_create("Audio Capture Queue", DISPATCH_QUEUE_SERIAL);

[audioOutput setSampleBufferDelegate:self queue:audioQueue];

if ([captureSession canAddOutput:audioOutput]) {

[captureSession addOutput:audioOutput];

}

// 9.获取视频输入与输出连接,用于分辨音视频数据

_videoConnection = [videoOutput connectionWithMediaType:AVMediaTypeVideo];

// 10.添加视频预览图层

AVCaptureVideoPreviewLayer *previedLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];

previedLayer.frame = [UIScreen mainScreen].bounds;

[self.view.layer insertSublayer:previedLayer atIndex:0];

_previedLayer = previedLayer;

// 11.启动会话

[captureSession startRunning];

}

// 指定摄像头方向获取摄像头

- (AVCaptureDevice *)getVideoDevice:(AVCaptureDevicePosition)position

{

NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

for (AVCaptureDevice *device in devices) {

if (device.position == position) {

return device;

}

}

return nil;

}

#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate

// 获取输入设备数据,有可能是音频有可能是视频

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection

{

if (_videoConnection == connection) {

NSLog(@"采集到视频数据");

} else {

NSLog(@"采集到音频数据");

}

}

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

你可能感兴趣的文章
Facebook图片存储系统Haystack——存小文件,本质上是将多个小文件合并为一个大文件来降低io次数,meta data里存偏移量...
查看>>
Bluemix中的Apache Spark数据分析服务入门
查看>>
小龟区块链中国行:世界需要区块链编织成网、互相协作
查看>>
面试集锦(十)支付宝与微信
查看>>
SpringBoot 框架MongoDB 简单的查询方式
查看>>
js浮点数丢失问题
查看>>
利用Python对QQ空间数据进行分析,了解你的QQ好友
查看>>
SQL Server [备份恢复]:完整备份,差异备份或事务日志备份,尾部日志备份
查看>>
System Center 2012 R2实例2—构建Azure Pack云13—租户NAT
查看>>
Git基础入门(七)Git撤销操作和远程仓库管理
查看>>
开始OpenCV之旅
查看>>
解决github.com/mattn/go-sqlite3 驱动中的utc时区变为本地系统时...
查看>>
Fabric.js高级点的教程1--添加辅助线的方法
查看>>
2011年 Linux 故事 Top 5
查看>>
ARouter 源码历险记 (五)
查看>>
优化Angular应用的性能
查看>>
php字符串函数
查看>>
[IOS] 自颁发证书不合法问题
查看>>
MYSQL常用命令
查看>>
Java中使用Jedis操作Redis
查看>>