iOS-音视频采集 by AVFoundation
2017-09-12 17:27
344 查看
在 iOS 开发中,二维码的扫码、拍照、录制视频,以及
在
使用系统自带的
使用第三方
使用
iOS 11推出的
AR 技术等都需要调用系统相机
在
iOS 8.0后,调用相机采集视频有两种常见方式:
使用系统自带的
AVFoundation框架
使用第三方
GPUImage框架
使用 AVFoundation
完成音视频的采集和显示
主要步骤
// 创建捕捉会话 captureSession = AVCaptureSession() // 设置视频输入源&输出源 setupVideoInputOutput() // 设置音频输入源&输出源 setupAudioInputOutput() // 添加预览图层 setupPreviewLayer() // 开始采集 captureSession?.startRunning() // 通过代理获取采集到的音视频 - CMSampleBuffer captureOutput(_ captureOutput: AVCaptureOutput!, didDrop sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) // 切换前后置摄像头 switchCamera() // 停止采集 captureSession?.stopRunning()
设置视频输入源&输出源
func setupVideoInputOutput() { // iOS 10.0 + // let device = AVCaptureDevice.defaultDevice(withDeviceType: AVCaptureDeviceType(rawValue: AVMediaTypeVideo), mediaType: AVMediaTypeVideo, position: .front) // 取出所有的设备 guard let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) as? [AVCaptureDevice] else { return } // 过滤出前置摄像头 guard let device = devices.filter({ $0.position == .front }).first else { return } // 创建输入设备 guard let input = try? AVCaptureDeviceInput(device: device) else { return } self.videoInput = input // 创建输出设备 let output = AVCaptureVideoDataOutput() output.setSampleBufferDelegate(self, queue: DispatchQueue.global()) self.videoOutput = output // 添加输入输出设备 if let session = captureSession { if session.canAddInput(input) { session.addInput(input) } if session.canAddOutput(output) { session.addOutput(output) } } }
设置音频输入源&输出源
func setupAudioInputOutput() { // 取出所有的设备 guard let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio) else { return } // 创建输入设备 guard let input = try? AVCaptureDeviceInput(device: device) else { return } // 创建输入设备 let output = AVCaptureAudioDataOutput() output.setSampleBufferDelegate(self, queue: DispatchQueue.global()) // 添加输入输出设备 if let session = captureSession { if session.canAddInput(input) { session.addInput(input) } if session.canAddOutput(output) { session.addOutput(output) } } }
添加预览图层
func setupPreviewLayer() { guard let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) else { return } previewLayer.frame = view.bounds self.previewLayer = previewLayer view.layer.insertSublayer(previewLayer, at: 0) }
转换摄像头
func switchCamera() { // 添加切换动画 let rotaionAnim = CATransition() rotaionAnim.type = "oglFlip" rotaionAnim.subtype = "fromLeft" rotaionAnim.duration = 0.25 view.layer.add(rotaionAnim, forKey: nil) // 获取当前设备的的摄像头方向 guard let videoInput = videoInput else { return } // 获取当前摄像头方向 并切换 let position: AVCaptureDevicePosition = videoInput.device.position == .front ? .back : .front guard let devices = (AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) as? [AVCaptureDevice]) else { return } guard let device = devices.filter({ $0.position == position }).first else { return } guard let newInput = try? AVCaptureDeviceInput(device: device) else { return } /// 开启配置 // 开始配置 captureSession?.beginConfiguration() captureSession?.removeInput(videoInput) captureSession?.addInput(newInput) // 结束配置 captureSession?.commitConfiguration() // 记录最新的 input self.videoInput = newInput }
停止音视频采集
func stopCapturing() { // 先移除预览视图 self.previewLayer?.removeFromSuperlayer() // 停止视频采集 captureSession?.stopRunning() // 置空 session captureSession = nil }
获取采集到的音视频
// MARK: - AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAudioDataOutputSampleBufferDelegate extension ViewController: AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAudioDataOutputSampleBufferDelegate { func captureOutput(_ captureOutput: AVCaptureOutput!, didDrop sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) { if self.videoOutput?.connection(withMediaType: AVMediaTypeVideo) == connection { print("采集到视频文件") } else { print("采集到音频文件") } } }
相关文章推荐
- ios avfoundation 合并音频到一个视频文件 avurlassettrack
- iOS-音视频采集 by GPUImage
- ios avfoundation 合并音频到一个视频文件 avurlassettrack
- iOS 加载网络视频之(AVFoundation.framewor)的MPMoviePlayerController
- iOS AVFoundation视频编辑
- How to capture video frames from the camera as images using AV Foundation on iOS
- iOS 视频采集参数定义
- iOS 对网络视频采集视频截图
- iOS RTMP 视频直播开发笔记(1) – 采集摄像头图像
- iOS 保存视频AVAssetWriter
- [iOS文档翻译]AVFoundation Programming Guide - About AVFoundation - AVFoundation概述
- iOS视频采集(二)
- 【iOS开发】关于视频直播技术,你想要知道的都在这里了(一)采集
- iOS 7 What’s New in AV Foundation之二维码扫描(上)
- iOS-Foundation框架(视频教程)
- IOS音频3:之采用四种方式播放音频文件(三)AudioToolbox AVFoundation OpenAL AUDIO QUEUE
- iOS音视频的采集
- IOS AVFoundation的录音与播放
- iOS基于AVFoundation + GPUImage的实时图像处理
- iOS视频采集(四)