Using AVCaptureVideoDataOutput and AVCaptureAudioDataOutput

1.8k Views Asked by At

I have an AVCaptureVideoDataOutput session running set up as below, which works great and records the buffer to a file.

I want to also record the audio but there doesn't seem to be any in the buffer even though I've added the microphone as an input to the captureSession.

I suspect I need to also use 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()
}

and then here's how I get the video buffer and send it off to be written to a file

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

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

}
1

There are 1 best solutions below

0
On

Check func captureOutput(AVCaptureOutput, didOutput: CMSampleBuffer, from: AVCaptureConnection) of AVCaptureAudioDataOutputSampleBufferDelegate if you want grab sample data of audio from AVCaptureAudioDataOutput https://developer.apple.com/documentation/avfoundation/avcaptureaudiodataoutputsamplebufferdelegate