For some reason, my voice recorder app won\'t use AirPods as an input source. Upon beginning a recording, the input source changes from the AirPods to the iPhone microphone.
It is worked for me
try? AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: [.allowBluetooth, .allowBluetoothA2DP]
if let headphones = AVAudioSession.sharedInstance().availableInputs?.first(where: { $0.portType == .bluetoothHFP || $0.portType == .bluetoothA2DP || $0.portType == .headphones }) {
try? AVAudioSession.sharedInstance().setPreferredInput(headphones)
}
try? AVAudioSession.sharedInstance().setActive(true)
Firstly i catch available output source.
let currentOutputSource = AVAudioSession.sharedInstance().outputDataSource
try? AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: [.allowBluetooth, .allowBluetoothA2DP])
if let currentOutputSource = currentOutputSource {
// Then i set known output source to input source.
try AVAudioSession.sharedInstance().setInputDataSource(currentOutputSource)
}
try? AVAudioSession.sharedInstance().setActive(true)
There are two ways to record and play audio while AirPods are connected. For both cases we will use AVAudioSessionCategoryPlayAndRecord
category. The trick is to manage AVAudioSession
s option set.
.allowBluetoothA2DP
option. This will allow you to listen audio via AirPods and record with internal mic..allowBluetooth
option. With this option you can record audio with AirPods during playback. But in this case audio quality will be reduced.Eddie, If you're still having the issue, please see a similar post I made that we were able to finally resolve the issues we were having.
AVAudioRecorder/AVAudioSession with Apple Airpods