How to capture and preview microphone audio during a multi-route audiosession.

Oscar de la Hera
AR Tips And Tricks
Published in
1 min readDec 7, 2018

For TLDR; Jump straight to Solution.

My audio sidekick.

Problem

For you that do not know me — I constantly have headphones connected to my device. I never thought this would be a problem until I saw that my app would freeze when replaying video — often illustrating this:

Unknown selected data source for Port delasounds (type: BluetoothA2DPOutput)

Theory

If it can capture some audio and freeze and other — then perhaps it is :

  • That you need to allow it to work in tandem with the speaker (tried and only led to more problems).

OR

  • That you have deactivated the ability for the app to play audio too early — jamming the video (this one is the case)

Solution

A : Setup your Audio Session as follows

do {   try self.recordingSession.setCategory(AVAudioSession.Category.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])   try self.recordingSession.setActive(true, options: .notifyOthersOnDeactivation)}
catch {
// CATCH THE ERROR
}

B : End the capturing of audio when you want your recording to stop.

func endAudioCapture() {    self.captureSession!.stopRunning();}

C: Turn the audio session “off” when you’re done playing the replay.

public func endAudioSession() {    do {    try self.recordingSession.setActive(false, options: .notifyOthersOnDeactivation)    }    catch {    print("ERROR CANCELLING RECORDING SESSION : \(error)")    }}

O

--

--

Oscar de la Hera
AR Tips And Tricks

Oscar is an award-winning Spanish Inventor whose work impacts lives through brands that include Nike, MoMA and Samsung.