如何在iOS设备上检测耳机(扬声器)的可用性?

我想在iPad上特别检测是否有耳机可用。

例如 – 我可以检测iOS设备是否具有使用AVFoundation的Turch,因此有没有办法检测耳机的可用性。

斯威夫特3:

import AVFoundation let currentRoute = AVAudioSession.sharedInstance().currentRoute for description in currentRoute.outputs { if description.portType == AVAudioSessionPortLineOut { }else if description.portType == AVAudioSessionPortHeadphones { }else if description.portType == AVAudioSessionPortBluetoothA2DP{ }else if description.portType == AVAudioSessionPortBuiltInReceiver{ }else if description.portType == AVAudioSessionPortBuiltInSpeaker{ }else if description.portType == AVAudioSessionPortHDMI{ }else if description.portType == AVAudioSessionPortAirPlay{ }else if description.portType == AVAudioSessionPortBluetoothLE{ } } 

参考:

Apple文档: https : //developer.apple.com/reference/avfoundation/avaudiosessionportdescription/1669281-output_port_types

1)如果要检查设备上是否有耳机(接收器扬声器)可用

您可以通过简单地识别Device是否为iPhone来识别这一点。

UIDevice.current.userInterfaceIdiom == .phone

在iOS prottype中AVAudioSessionPortBuiltInReceiver用于builtInreceriver扬声器。 根据苹果的文档 ,这仅适用于iPhone设备。 因此,没有必要检查其他任何东西,如果它的iPhone,你有耳机片,如果它不是iPhone(在ipad上)它没有耳机。

2)如果要检查是否连接了手机:

您可以使用共享音频会话的电流来检查耳机是否已连接:这是swift 3.0中的示例function

  func IsHeadSetConnected() -> Bool{ let route = AVAudioSession.sharedInstance().currentRoute; for desc in route.outputs { let portType = desc.portType; if (portType == AVAudioSessionPortHeadphones) { return true; } } return false; } 

您还应该通过监听路由更改来监控其状态:

 NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange(_:)), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil) 

这是上面的通知设置处理程序的示例代码:

 func handleRouteChange(_ notification: Notification) { guard let userInfo = notification.userInfo, let reasonRaw = userInfo[AVAudioSessionRouteChangeReasonKey] as? NSNumber, let reason = AVAudioSessionRouteChangeReason(rawValue: reasonRaw.uintValue) else { fatalError("Strange... could not get routeChange") } switch reason { case .oldDeviceUnavailable: print("oldDeviceUnavailable") case .newDeviceAvailable: print("headset/line plugged in") case .routeConfigurationChange: print("headset pulled out") case .categoryChange: print("Just category change") default: print("not handling reason") } } 

最简单的方法是检查via contains

 func isHeadphonesConnected() -> Bool{ let routes = AVAudioSession.sharedInstance().currentRoute return routes.outputs.contains(where: { (port) -> Bool in port.portType == AVAudioSessionPortHeadphones }) } 

检查

 if let availableInputs = AVAudioSession.sharedInstance().availableInputs { for route in availableInputs { if ( route.portType == AVAudioSessionPortBuiltInMic ) { //built-in-mic available break; } } } 

您可以检查内置麦克风是否可用。

 var isMicAvailable = false if let availableInputs = AVAudioSession.sharedInstance().availableInputs { for route in availableInputs { if ( route.portType == AVAudioSessionPortBuiltInMic ) { //built-in-mic available isMicAvailable = true break; } } } print("current device has mic - \(isMicAvailable)") 

另一种检查当前设备是否为iPhone的方法。

 NSString *deviceType = [UIDevice currentDevice].model; if([deviceType isEqualToString:@"iPhone"]) { //current device is iPhone. } 

一个内衬Swift 4+解决方案:

检测内置麦克风是否可用

 let isMicAvailable: Bool = AVAudioSession.sharedInstance().availableInputs?.first(where: { $0.portType == AVAudioSessionPortBuiltInMic }) != nil 

检测耳机(接收器扬声器)是否可用

 let isEarPieceAvailable: Bool = AVAudioSession.sharedInstance().currentRoute.outputs.first(where: { $0.portType == AVAudioSessionPortBuiltInReceiver }) != nil