Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioSession - How to switch between speaker and headphones output

I'm trying to mimic behaviour as in Phone app during calling. You can easily switch output sources from/to speaker or headphones. I know I can force speaker as an output when headphones are connected by calling:

try! audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)  
try! audioSession.overrideOutputAudioPort(.speaker)  

However, when I do that, I don't see any way to detect if headphones are still connected to the device.

I initially thought outputDataSources on AVAudioSession would return all posible outputs but it always returns nil.

Is there something I'm missing

like image 637
msmialko Avatar asked Sep 18 '18 16:09

msmialko


People also ask

Why is my speakers and headphones working at the same time?

If you have multiple audio output devices connected to your Windows 10 PC, your device might stream the sound in the wrong output even after you plug in your headphones. To prevent this from happening, set your headphones as your PC's default audio device.


1 Answers

You need to change the outputDataSources, as when you overrode it,

now it contains only the .Speaker option

in the Documentation you can find the solution to this,

If your app uses the playAndRecord category, calling this method with the AVAudioSession.PortOverride.speaker option causes audio to be routed to the built-in speaker and microphone regardless of other settings. This change remains in effect only until the current route changes or you call this method again with the AVAudioSession.PortOverride.none option.

Therefore the audio is routed to the built-in speaker, This change remains in effect only untill the current route changes or you call this method again with .noneOption.

it's not possible to forcefully direct sound to headphone unless an accessory is plugged to headphone jack (which activates a physical switch to direct voice to headphone).

So when you want to switch back to headphone, this should work. And if there is no headphone connected will switch the output device to the small speaker output on the top of the device instead of the big speaker.

let session: AVAudioSession = AVAudioSession.sharedInstance()
        do {
            try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
            try session.overrideOutputAudioPort(AVAudioSession.PortOverride.none)
            try session.setActive(true)
        } catch {
            print("Couldn't override output audio port")
        }

Read about this AVAdioSession/OverrideOutputAudioPort Here.

You can check if headset connected adding this extension,

    extension AVAudioSession {

    static var isHeadphonesConnected: Bool {
        return sharedInstance().isHeadphonesConnected
    }

    var isHeadphonesConnected: Bool {
        return !currentRoute.outputs.filter { $0.isHeadphones }.isEmpty
    }

}

extension AVAudioSessionPortDescription {
    var isHeadphones: Bool {
        return portType == AVAudioSessionPortHeadphones
    }
}

And simply use this line of code

session.isHeadphonesConnected
like image 179
Mohmmad S Avatar answered Sep 19 '22 11:09

Mohmmad S