Your AVCaptureSession goes beyond and is freed. That is why your delegate is not being called. You can fix this by moving session to the scope of the class:
class ViewController: UIViewController, AVCaptureAudioDataOutputSampleBufferDelegate { let session = AVCaptureSession() override func viewDidLoad() {
Once you have the CMSampleBuffer audio, you can copy the audio data to an NSData object as follows:
let block = CMSampleBufferGetDataBuffer(sampleBuffer) var length = 0 var data: UnsafeMutablePointer<Int8> = nil let status = CMBlockBufferGetDataPointer(block!, 0, nil, &length, &data)
ps if you are careful and want to avoid copying, you can use NSData(bytesNoCopy: data, length: length, freeWhenDone: false)
source share