AVAudioSession-如何在扬声器和耳机输出之间切换 [英] AVAudioSession - How to switch between speaker and headphones output

查看:404
本文介绍了AVAudioSession-如何在扬声器和耳机输出之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在通话过程中模仿电话"应用程序中的行为.您可以轻松地在扬声器或耳机之间切换输出源. 我知道当通过以下方式连接耳机时,我可以强制扬声器作为输出:

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.

我最初以为AVAudioSession上的outputDataSources将返回所有可能的输出,但始终返回nil.

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

有什么我想念的吗

推荐答案

您需要更改outputDataSources,就像您覆盖它时一样

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

现在它仅包含.Speaker选项

在文档中,您可以找到解决方案,

in the Documentation you can find the solution to this,

如果您的应用使用playAndRecord类别,则使用AVAudioSession.PortOverride.speaker选项调用此方法将导致将音频路由到内置扬声器和麦克风,而不考虑其他设置.该更改仅在当前路由更改之前有效,或者您使用AVAudioSession.PortOverride.none选项再次调用此方法.

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.

因此,音频被路由到内置扬声器,此更改仅在当前路由更改或您使用.noneOption再次调用此方法之前一直有效.

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")
        }

了解此AVAdioSession/OverrideOutputAudioPort 此处.

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
    }
}

只需使用此行代码

session.isHeadphonesConnected

这篇关于AVAudioSession-如何在扬声器和耳机输出之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆