Receive iPhone Microphone Data for Socket Streaming

I would like to receive unprocessed audio data from an iPhone microphone (in NSData format) for streaming over a socket. This is not a situation where I can use twilio / etc, as this is a research project. The socket implementation is in progress (I can send audio files), but I can’t get the stream microphone data.

Here is my attempt:

class ViewController: UIViewController, AVCaptureAudioDataOutputSampleBufferDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.setupMicrophone() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func setupMicrophone() { let session = AVCaptureSession() session.sessionPreset = AVCaptureSessionPresetMedium let mic = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio) var mic_input: AVCaptureDeviceInput! let audio_output = AVCaptureAudioDataOutput() audio_output.setSampleBufferDelegate(self, queue: dispatch_get_main_queue()) do { mic_input = try AVCaptureDeviceInput(device: mic) } catch { return } session.addInput(mic_input) session.addOutput(audio_output) session.startRunning() } func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) { // Do something here } } 

Problems:

  • The delegate function is never called.

  • The data provided to the delegate (if called) is not NSData, is there another function that NSData provided? Is there a way to convert CMSampleBuffer to NSData?

Any help is appreciated.

Greetings

+2
source share
1 answer

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) // TODO: check for errors let result = NSData(bytes: data, length: length) 

ps if you are careful and want to avoid copying, you can use NSData(bytesNoCopy: data, length: length, freeWhenDone: false)

0
source

All Articles