We are developing a voice call application that uses AVRoutePickerView, allowing users to switch between an iPhone, a speaker, and Bluetooth earphones. However, we have encountered an intermittent issue: when switching from the speaker to Bluetooth earphones, the earphones remain in a loading state, and the audio channel subsequently switches back to the speaker. How can we resolve this issue? Here is the sample code:
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(.playAndRecord, mode: .default, options: [.allowBluetooth])
try session.overrideOutputAudioPort(isBluetoothConnected ? .none : .speaker)
try session.setPreferredSampleRate(48000)
try session.setPreferredIOBufferDuration(0.02)
try session.setActive(true, options: [])
INFO(" >>> start: hasBluetooth=\(isBluetoothConnected), inputs: \(session.currentRoute.inputs.map { $0.portType }) outputs: \(session.currentRoute.outputs.map { $0.portType })")
} catch {
ERROR("(error.localizedDescription)")
}