iOS: 如何检查扬声器是否打开

iOS: how to check if the speaker is on

我使用AVAudioSessionoverrideOutputAudioPort方法在iOS的扬声器中播放声音。 overrideOutputAudioPort

如何检查输出音频是否被覆盖为扬声器,总之声音是否由扬声器播放?

您可以使用 currentRoute 检查它 请考虑以下代码:

let currentRoute = AVAudioSession.sharedInstance().currentRoute

    for output in currentRoute.outputs {

        switch output.portType {

        case AVAudioSessionPortBuiltInSpeaker:
            print("Speaker is on.")
        default:
            break
        }
    }

您可以参考 Apple Doc 了解更多信息。

而原来的post是here。但它带有 if 检查,但根据我的说法,switch 更好用。

并且您可以检测 apple doc 中提到的其他输出。