使用AVCaptureVideoDataOutput和AVCaptureAudioDataOutput

时间:2017-05-24 16:09:23

标签: ios swift avcapturesession avcaptureoutput

我有一个AVCaptureVideoDataOutput会话,运行设置如下,效果很好,并将缓冲区记录到文件中。

我还要录制音频,但即使我已将麦克风添加为captureSession的输入,但缓冲区似乎也没有。

我怀疑我还需要使用AVCaptureAudioDataOutput。

func setupCaptureSession()
{
captureSession.beginConfiguration()
captureSession.sessionPreset = AVCaptureSessionPreset1280x720
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "sample 
buffer delegate", attributes: []))


videoOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString) : NSNumber(value: kCVPixelFormatType_32BGRA as UInt32)]

videoOutput.alwaysDiscardsLateVideoFrames = true

captureSession.addOutput(videoOutput)

captureSession.addInput(deviceInputFromDevice(backCameraDevice))

captureSession.addInput(deviceInputFromDevice(micDevice))

captureSession.commitConfiguration()
captureSession.startRunning()
}

然后在这里我如何获取视频缓冲区并将其发送以写入文件

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!)
{

writeVideoFromData(sampleBuffer) // a function that writes the buffer to disk

}

1 个答案:

答案 0 :(得分:0)

如果您想从AVCaptureAudioDataOutput获取音频样本数据,请检查 AVCaptureAudioDataOutputSampleBufferDelegate func captureOutput(AVCaptureOutput, didOutput: CMSampleBuffer, from: AVCaptureConnection) https://developer.apple.com/documentation/avfoundation/avcaptureaudiodataoutputsamplebufferdelegate