In our iOS application, we have a custom headset without a microphone. We want users to listen through this headset while speaking via an ipad microphone. However, when we use the default route-changing code in Swift, the iOS system recognizes the iPad as a complete device and routes the audio output through the iPad’s speaker instead.
How can we improve this to ensure that audio input is correctly routed to the ipad microphone while allowing audio playback through our custom headset?
do {
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(AVAudioSession.Category.playAndRecord, options: [.allowBluetoothA2DP])
try audioSession.setActive(true)
}catch {
print("Failed to set up audio session: \(error)")
}
do {
let availableInputs = try audioSession.availableInputs
if let inputs = availableInputs {
for input in inputs {
if input.portType == .bluetoothHFP || input.portType == .headsetMic {
try audioSession.setPreferredInput(input)
break
}
}
}
} catch {
print(“Failed to configure audio routing: (error)”)
}
}