AVAudioSessionRouteChangeReason

AVAudioSessionRouteChangeReason

如何从应用程序内部检测设备是否插入了外部麦克风?

最佳答案

试试这个:

let route = AVAudioSession.sharedInstance().currentRoute

for port in route.outputs {
    if port.portType == AVAudioSessionPortHeadphones {
        // Headphones located
    }
}

编辑:发布有问题的OP更改-

当应用程序运行时,您需要注册AVAudioSessionRouteChangeNotification来监听这样的更改:
NSNotificationCenter.defaultCenter().addObserver(self, selector:"audioRouteChangeListener:", name: AVAudioSessionRouteChangeNotification, object: nil)

dynamic private func audioRouteChangeListener(notification:NSNotification) {
    let audioRouteChangeReason = notification.userInfo![AVAudioSessionRouteChangeReasonKey] as UInt

    switch audioRouteChangeReason {
    case AVAudioSessionRouteChangeReason.NewDeviceAvailable.rawValue:
        println("headphone plugged in")
    case AVAudioSessionRouteChangeReason.OldDeviceUnavailable.rawValue:
        println("headphone pulled out")
    default:
        break
    }
}

关于ios - 如何在iOS中检测外部麦克风?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33344022/

10-13 03:50