2

I am working on a Video/Audio Call App where i need to provide four options related to the Audio Output:

Speaker, Built in mic, Any BLE Device supporting audio, No Audio output

Below functions i have used:

static func setBuiltInMic() {
        let outputs = audioSession.availableInputs
        for output in outputs! {
            if output.portType.rawValue == AVAudioSession.Port.builtInMic.rawValue {
                do {
                    try audioSession.setPreferredInput(output)
                } catch let error {
                    print("Setting Built in Mic Port: \(error.localizedDescription)")
                }
            }
        }
    }
 static func setAndCheckBLEAudioPort() -> Bool {
        let outputs = audioSession.availableInputs
        for output in outputs! {
            if output.portType.rawValue == AVAudioSession.Port.bluetoothHFP.rawValue {
                do {
                    try audioSession.setPreferredInput(output)
                    return true
                } catch let error {
                    print("Setting BLE Port: \(error.localizedDescription)")
                    return false
                }
            }
        }
        return false
    }
static func setupAudioSession(isSpeakerEnabled: Bool) {
        do {
            try audioSession.setCategory(.playAndRecord)
            try audioSession.setMode(.voiceChat)
            try audioSession.overrideOutputAudioPort(isSpeakerEnabled ? .speaker : .none)
            try audioSession.setActive(true, options: [])
        } catch let error as NSError {
            print("Fail: \(error.localizedDescription)")
        }
    }

But this doesn't work Audio keeps coming from different source like speaker even if i try to mute it using setupAudioSession

Anyone has an idea or reference for me to look into it?

philnash
  • 70,667
  • 10
  • 60
  • 88
Jatin Garg
  • 306
  • 1
  • 2
  • 16
  • I didn´t debug this but `let outputs = audioSession.availableInputs` seems wrong to me. – burnsi May 21 '22 at 14:13
  • This generally returns me the port type like I’ve airpods on then it returns me bluetooth type and name of the airpods and if not this returns as Speaker. Do you have any ref for me to try if this seems to be wrong to you. – Jatin Garg May 21 '22 at 15:08
  • You are assigning input to output devices this seems odd to me. But no ref. – burnsi May 21 '22 at 15:27
  • I can try with outputs if i could get but both will have reference of port which will be assigned. – Jatin Garg May 21 '22 at 15:38

1 Answers1

0

The code is working fine, it was an issue with a third party library used for audio and video calls prior to Twilio.

Jatin Garg
  • 306
  • 1
  • 2
  • 16