When I run AVCaptureSession with AVCaptureAudioDataOutput, the delegate is not being called. What step am I missing? I appreciate your help!
import Cocoa
import AVFoundation
class ViewController: NSViewController, AVCaptureAudioDataOutputSampleBufferDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let session = AVCaptureSession()
guard let device = AVCaptureDevice.default(.builtInMicrophone, for: .audio, position: .front) else { return}
do {
let input = try AVCaptureDeviceInput(device:device)
if session.canAddInput(input) {
session.addInput(input)
let output = AVCaptureAudioDataOutput()
output.setSampleBufferDelegate(self, queue: .main)
if session.canAddOutput(output) {
session.addOutput(output)
}
session.startRunning()
print("Running")
}
} catch {
}
}
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
print(sampleBuffer.numSamples)
}
}
Did you get any devices? I tried to build your code but didn't get any devices. Anyway, you can look at this answer and also this answer. Also, one thing I can guess, not sure though. Put your capture session variable
let session = AVCaptureSession()
outside of viewDidLoad.