//
// AudioVideoCaptureViewController.m
// live
//
// Created by lujunjie on 2016/10/31.
// Copyright © 2016年 lujunjie. All rights reserved.
// #import "AudioVideoCaptureViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface AudioVideoCaptureViewController ()<AVCaptureVideoDataOutputSampleBufferDelegate,AVCaptureAudioDataOutputSampleBufferDelegate>
@property (nonatomic,strong) AVCaptureSession *mCaptureSession;
@property (nonatomic,strong) AVCaptureDeviceInput *mCaptureDeviceInput;
@property (nonatomic ,strong) AVCaptureDeviceInput *mCaptureAudioDeviceInput;//负责从AVCaptureDevice获得输入数据
@property (nonatomic,strong) AVCaptureVideoDataOutput *mCaptureVideoOutput;
@property (nonatomic , strong) AVCaptureAudioDataOutput *mCaptureAudioOutput;
@property (nonatomic,strong) dispatch_queue_t mProcessQueue;
@property (nonatomic,strong) dispatch_queue_t mCaptureQueue;
@property (nonatomic,strong) dispatch_queue_t mEncodeQueue; @property (nonatomic,strong) AVCaptureVideoPreviewLayer *mPreviewLayer;
@end @implementation AudioVideoCaptureViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. // a、AVCaptureDevice。这里代表抽象的硬件设备。
//
// b、AVCaptureInput。这里代表输入设备(可以是它的子类),它配置抽象硬件设备的ports。
//
// c、AVCaptureOutput。它代表输出数据,管理着输出到一个movie或者图像。
//
// d、AVCaptureSession。它是input和output的桥梁。它协调着intput到output的数据传输。
[self startCapture];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)startCapture {
// 新建会话,设置图像大小
self.mCaptureSession = [[AVCaptureSession alloc] init];
self.mCaptureSession.sessionPreset = AVCaptureSessionPreset640x480; self.mCaptureQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );
self.mEncodeQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); AVCaptureDevice *inputCamera = nil;
// 获取前置摄像头
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices)
{
if ([device position] == AVCaptureDevicePositionFront)
{
inputCamera = device;
}
}
// 把摄像头设置到输入设备,并添加到会话
self.mCaptureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:inputCamera error:nil]; if ([self.mCaptureSession canAddInput:self.mCaptureDeviceInput]) {
[self.mCaptureSession addInput:self.mCaptureDeviceInput];
} // 设置输出设备的参数,并把输出设备添加到会话
self.mCaptureVideoOutput = [[AVCaptureVideoDataOutput alloc] init];
[self.mCaptureVideoOutput setAlwaysDiscardsLateVideoFrames:NO]; [self.mCaptureVideoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]]; [self.mCaptureVideoOutput setSampleBufferDelegate:self queue:self.mCaptureQueue];
if ([self.mCaptureSession canAddOutput:self.mCaptureVideoOutput]) {
[self.mCaptureSession addOutput:self.mCaptureVideoOutput];
}
// 输出设置缩放裁剪系数设置、建立视频音频链接
AVCaptureConnection *connection = [self.mCaptureVideoOutput connectionWithMediaType:AVMediaTypeVideo];
[connection setVideoOrientation:AVCaptureVideoOrientationPortrait]; // 视频
self.mPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.mCaptureSession];
[self.mPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspect]; //设置预览时的视频缩放方式
[self.mPreviewLayer setFrame:self.view.bounds];
[self.view.layer addSublayer:self.mPreviewLayer]; // 获取麦克风设备
AVCaptureDevice *audioDevice = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] lastObject];
// 把设备设置到输入设备,并添加到会话
self.mCaptureAudioDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioDevice error:nil];
if ([self.mCaptureSession canAddInput:self.mCaptureAudioDeviceInput]) {
[self.mCaptureSession addInput:self.mCaptureAudioDeviceInput];
}
// 设置输出设备的参数,并把输出设备添加到会话
self.mCaptureAudioOutput = [[AVCaptureAudioDataOutput alloc] init];
if ([self.mCaptureSession canAddOutput:self.mCaptureAudioOutput]) {
[self.mCaptureSession addOutput:self.mCaptureAudioOutput];
}
[self.mCaptureAudioOutput setSampleBufferDelegate:self queue:self.mCaptureQueue]; [self.mCaptureSession startRunning];
} - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
if (captureOutput == self.mCaptureVideoOutput) {
dispatch_sync(self.mEncodeQueue, ^{
NSLog(@"视频:::sampleBuffer");
});
}
else {
dispatch_sync(self.mEncodeQueue, ^{
NSLog(@"音频:::sampleBuffer");
});
}
} @end

AVCaptureSession音频视频采集的更多相关文章

  1. iOS 直播-获取音频(视频)数据

    iOS 直播-获取音频(视频)数据 // // ViewController.m // capture-test // // Created by caoxu on 16/6/3. // Copyri ...

  2. 第六十篇、音视频采集硬编码(H264+ACC)

    使用 AVCaptureSession进行实时采集音视频(YUV.),编码 通过AVCaptureVideoDataOutputSampleBufferDelegate获取到音视频buffer- 数据 ...

  3. 直播软件开发关于Android、iOS中的视频采集步骤

    很多人对直播软件开发还是抱有想法的,但是在这个资本冷静的市场下,直播平台该怎么玩,在直播软件开发过程中哪些功能是必须具备的,这都是值得关注的话题.今天我们给大家分享一份详细的直播软件开发关于Andro ...

  4. (三)WebRTC手记之本地视频采集

    转自:http://www.cnblogs.com/fangkm/p/4374610.html 前面两篇文章介绍WebRTC的运行流程和使用框架接口,接下来就开始分析本地音视频的采集流程.由于篇幅较大 ...

  5. WebRTC手记之本地视频采集

    转载请注明出处:http://www.cnblogs.com/fangkm/p/4374610.html 前面两篇文章介绍WebRTC的运行流程和使用框架接口,接下来就开始分析本地音视频的采集流程.由 ...

  6. Window 下 VFW 视频采集与显示

    引言 经过几天的努力终于将VFW视频采集与显示功能完整实现了,不得不说网上对这方面完整的详细讲解文章是在太少了.所以就要本人来好好总结一下让后来者不再像我一样折腾好久.在本文中我将详细讲解VFW视频采 ...

  7. Delphi XE6 试用Android视频采集

    FMX支持视频采集,具体见FMX.Media,提供了很类支持音频.视频的处理. 按帮助文档,用Note3做了测试,结果,效率太低,不可用. 具体可查询帮助Video Capturing一节,我就是按这 ...

  8. 嵌入式LINUX环境下视频采集知识

    V4L2是Linux环境下开发视频采集设备驱动程序的一套规范(API),它为驱动程序的编写提供统一的接口,并将所有的视频采集设备的驱动程序都纳入其的管理之中.V4L2不仅给驱动程序编写者带来极大的方便 ...

  9. 音频视频解决方案:GStreamer/ffmpeg/ffdshow/directshow/vfw

    音频视频编程相关:GStreamer/ffmpeg/directshow/vfw linux和window下几种流行的音频视频编程框架作一个总结,防止自己迷惘,免于晕头转向. 一.GStreamer ...

随机推荐

  1. 笔记本E450机械硬盘数据迁移到固态硬盘

    背景: E450机械硬盘使用速度过慢,但E450只有一个SATA位,无法直接使用 “分区助手”迁移. 处理: 1.将固态硬盘通过USB口外接在笔记本上 2.正常打开E450,进入桌面 3.对固态硬盘进 ...

  2. 如何修改Web.Config里面的值

    0.先添加 <add key="MAXNUM" value="6" /> 1.读取值 string maxNum = ConfigurationMa ...

  3. 【Henu ACM Round#17 E】Tree Construction

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 做这题之前先要知道二叉排序树的一个性质. 就是它的中序遍历的结果就是这个数组升序排序. (且每个节点的左边的节点都是比这个节点的值小 ...

  4. 多个ComboBox绑定同一个数据源出现的问题解决办法

    出现问题: 当多个ComboBox绑定同一个数据源后,只要更改其中一个的选择项时,其它的ComboBox也跟着改变了 解决办法: DataTable dt = new DataTable(); dt ...

  5. PMP杂谈--制约因素与如果条件

     制约因素和如果条件都是项目范围说明书的内容.它们是项目范围基准的一部分,是定义活动.估算活动持续时间.制定进度计划.估算成本.制定预算.识别风险和规划採购等多个过程的输入. 差别:制约因素是确定 ...

  6. Android 4.4 Fence在SurfaceFlinger中的应用

    网上关于android.fence的资料好少啊.差点儿没有,可是这个机制又在GUI系统中起着关键的数据,于是自己通读源代码和凝视.与大家分享下Fence究竟是怎么回事? Fence即栅栏.栅栏的角色与 ...

  7. amaze ui中的icon button

    amaze ui中的icon button 说明几点: 1.链接效果 连接效果的本质一般都是a标签,好像很多button的链接效果都是用的a标签,submit表单提交或者button的type为sub ...

  8. java中的九大隐藏变量.

          javax.servlet.jsp.JspWriter类型,代表输出流的对象.作用域为page(页面执行期) request:javax.servlet.ServletRequest的子类 ...

  9. VMwarep挂载镜像及配置本地Yum源

    1.挂载镜像: *. 通过mount命令         linux mount挂载设备(u盘,光盘,iso等 )使用说明 *.  通过VMware的控制页面手工挂载 1.1    打开Vmware软 ...

  10. groupmod---更改群组识别码或名称

    groupmod命令用于更改群组识别码或名称. 需要更改群组的识别码或名称时,可用groupmod指令来完成这项工作. 语法 groupmod [-g <群组识别码> <-o> ...