Apple Intelligence is now available on iPhone, iPad, and Mac!

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Audio Enroute Doesn't change to Speaker, Bluetooth Device, Mute or internal Speaker

Hi Devs,


i am struggling on a task where i need to switch the audio routing to various mediums such as bottom Speaker, Top speaker, Mute the Audio or a bluetooth Device connected such as Airpods or Car Audio.

I have using twilio to perform Video call across iPhone devices.

below code for setting up the session:

let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(.playAndRecord, mode: .voiceChat, options: [.defaultToSpeaker, .mixWithOthers, .allowBluetooth, .allowAirPlay, .allowBluetoothA2DP])
try audioSession.setActive(true)


Changing the Audio enroute using below function:


class func setAudioMode(as mode: AudioMode) {
        switch mode {
        case .speaker:
            do {
                print("Jatin Garg: Setting as Speaker")
                try audioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
            } catch let error {
                print("Jatin Garg : audioSession error turning on speaker: \(error.localizedDescription)")
            }
        case .mute:
            do {
                print("Jatin Garg: Setting as None")
                try audioSession.setPreferredInput(.none)
                try audioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.none)
            } catch let error {
                print("Jatin Garg : audioSession error turning on None: \(error.localizedDescription)")
            }
        case .bleSpeaker:
            let inputs = audioSession.availableInputs
            print("jatin garg: inputs: \(String(describing: audioSession.availableInputs))")
            for input in inputs ?? [] {
                if input.portType.rawValue == AVAudioSession.Port.bluetoothA2DP.rawValue || input.portType.rawValue == AVAudioSession.Port.bluetoothHFP.rawValue ||
                    input.portType.rawValue == AVAudioSession.Port.bluetoothLE.rawValue ||
                    input.portType.rawValue == AVAudioSession.Port.headphones.rawValue ||
                    input.portType.rawValue == AVAudioSession.Port.carAudio.rawValue ||
                    input.portType.rawValue == AVAudioSession.Port.headsetMic.rawValue {
                    do {
                        print("Jatin Garg: Setting as BLE Speaker")
                        DefaultAudioDevice.DefaultAVAudioSessionConfigurationBlock()
                        try audioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.none)
                        try audioSession.setPreferredInput(input)
                    } catch let error {
                        print("Jatin garg: Setting BLE Speaker Port: \(error.localizedDescription)")
                        setAudioMode(as: .speaker)
                    }
                }
            }
        default:
            let inputs = audioSession.availableInputs
            print("jatin garg: inputs: \(String(describing: audioSession.availableInputs))")
            for input in inputs ?? [] {
                if input.portType.rawValue == AVAudioSession.Port.builtInMic.rawValue ||
                    input.portType.rawValue == AVAudioSession.Port.builtInReceiver.rawValue {
                    do {
                        print("Jatin Garg: Setting as built in mic")
                        try audioSession.setPreferredInput(input)
                        try audioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.none)
                    } catch let error {
                        print("Jatin Garg : audioSession error turning on None: \(error.localizedDescription)")
                    }
                }
            }
        }
    }


Please help me what i am missing?

iPhone 11, iOS 15

Posted on Jun 10, 2022 8:54 PM

Reply
Question marked as Top-ranking reply

Posted on Jul 15, 2022 8:31 AM

There were one other third party integration done for the purpose of video call which was creating issue in the audio enroute setting.

Similar questions

1 reply

Audio Enroute Doesn't change to Speaker, Bluetooth Device, Mute or internal Speaker

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.