Record on built in mic and simultaneously playback processed audio on AirPods

Basically for this iPhone app I want to be able to record from either the built in microphone or from a connected USB audio device while simultaneously playing back processed audio on connected AirPods. It's a pretty simple AVAudioEngine setup that includes a couple of effects units. The category is set to .playAndRecord with the .allowBluetooth and .allowBluetoothA2DP options added. With no attempts to set the preferred input and AirPods connected, the AirPods mic will be used and output also goes to the AirPods. If I call setPreferredInput to either built in mic or a USB audio device I will get input as desired but then output will always go to the speaker. I don't really see a good explanation for this and overrideOutputAudioPort does not really seem to have suitable options.

Testing this on iPhone 14 Pro

Answered by carsten_gp in 790662022

Happy to report that my issue got solved via a WWDC lab appointment. Turns out that not including the .allowBluetooth option and only using .allowBluetoothA2DP is the solution

Looking at the route change notification that occurs the reason indicated is override which has the following interesting comments noted in it's definition:

/// The route has been overridden (e.g. category is AVAudioSessionCategoryPlayAndRecord and
        /// the output has been changed from the receiver, which is the default, to the speaker).
        case override = 4

Which basically describes my situation. So it seems to be on purpose - but why? I can see that others have struggled with the same issue in iOS 16 and have found that the only solution is writing Core Audio code in C, something I definitely have no desire to do - been there done that

Let me just add that FB13579667 has been created for this - including a trivial sample app demonstrating the issue

Accepted Answer

Happy to report that my issue got solved via a WWDC lab appointment. Turns out that not including the .allowBluetooth option and only using .allowBluetoothA2DP is the solution

Record on built in mic and simultaneously playback processed audio on AirPods
 
 
Q