美文网首页
直播的学习与使用-----采集

直播的学习与使用-----采集

作者: OwenKing | 来源:发表于2017-12-06 20:07 被阅读16次

// 捕获音视频

- (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(@"采集到音频数据");

}

}

相关文章

  • 直播的学习与使用-----采集

    // 捕获音视频 - (void)setupCaputureVideo { // 1.创建捕获会话,必须要强引用,...

  • iOS自定义音视频采集问题汇总

    公司的直播项目一直采用的网易云的直播SDK,后来产品需求需要我们使用手机端作为音视频采集设备,使用电视端作为传输与...

  • 重新学起

    直播大火,学习一下吧 要做直播 有8个步骤 1.视屏的采集 视频的采集可以用 iOS原生avfoundation ...

  • Android 直播专题2-音视频采集

    Android 直播专题2-音视频采集Android 直播专题2-音视频采集Android 直播专题2-音视频采集...

  • 直播的学习与使用-----播放

    直播原理:把直播录制的视频,推送到服务器,在由服务器发给观众看 直播环节:推流端(采集、美颜处理、编码、推流)、服...

  • 直播 (一) : 直播的技术分析与实现

    HTTP Live Streaming直播(iOS直播)技术分析与实现 HLS技术要点分析 1.采集视频源和音频源...

  • iOS直播视频技术概述一

    直播技术概况来说,可以分为 采集,前处理,编码,传输,解码,渲染 这几个环节 音视频采集 音视频的采集是直播架构的...

  • Android使用蓝牙耳机MIC采集音频

    今天在直播项目中,遇到当主播使用蓝牙耳机直播的过程中,依旧使用手机的MIC采集音频,当主播距离手机比较远的时候,收...

  • iOS ReplayKit 屏幕共享,屏幕直播实现

    使用replayKit iOS12 之后相关 api 完成系统/app 内 屏幕采集直播视频数据, 采用 sock...

  • 直播架构浅析

    前言 直播流程模型如下图 1.采集 采集是整个直播过程中的第一个环节,负责从采集设备中采集原始数据传递到下一个环节...

网友评论

      本文标题:直播的学习与使用-----采集

      本文链接:https://www.haomeiwen.com/subject/cpwlixtx.html