AVAudioSession - How to switch between speaker and headphones output

北城余情 提交于 2020-06-27 09:06:06

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/52390659/avaudiosession-how-to-switch-between-speaker-and-headphones-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!