检测连接的音频设备 iOS [英] Detect attached audio devices iOS
问题描述
我正在尝试弄清楚如何检测 iphone/ipad/ipod 上是否连接了任何音频设备.我知道所有关于音频路由调用和路由更改回调的信息,但这些并没有告诉我关于附加内容的任何信息.他们只报告音频当前路由的位置.例如,我需要知道,当音频通过扬声器路由时,耳机和/或蓝牙是否仍然连接.或者,例如,如果用户在使用蓝牙时插入耳机然后决定断开蓝牙连接,我需要知道蓝牙已断开连接,即使音频仍在通过耳机路由.
I'm trying to figure out how to detect which if any audio devices are connected on iphone/ipad/ipod. I know all about the audio route calls and route change callbacks but these don't tell me anything about what's attached. They only report where the audio is currently routing. I need to know, for instance, if headphones and/or bluetooth are still attached while audio is routed through the speakers. Or, for instance, if a user plugs in the headset while using bluetooth then decides to disconnect bluetooth, I need to know that the bluetooth is disconnected even as audio is still routing through headphones.
推荐答案
不幸的是,从 iOS11 开始,似乎没有 API 可以可靠地获取当前附加的输出设备列表 - 一旦当前路由发生变化,您只能通过 AVAudioSession 的 currentRoute.outputs
看到 1 个设备(当前路由),即使可能仍然连接了多个设备.
Unfortunately, as of iOS11, it seems there's no API to reliably get the list of the output devices that are currently attached - as soon as the current route changes, you only see 1 device (currently routed) via AVAudioSession's currentRoute.outputs
, even though multiple devices may still be attached.
但是,对于输入,包括具有 HFP 配置文件的蓝牙设备,如果使用正确的音频会话模式(例如AVAudioSessionModeVoiceChat
或 AVAudioSessionModeVideoChat
),则可以通过 AVAudioSession 的 availableInputs
获取可用输入的列表,即使该设备不是活动路由,这些输入也会列在那里 - 这在用户通过 进行手动覆盖时非常有用以MPVolumeView
从蓝牙到音箱为例,由于HFP是2路IO(有输入和输出),你可以通过看输入判断输出HFP蓝牙是否仍然可用.
However, for the input, and that includes Bluetooth devices with HFP profile, if the proper Audio Session mode is used (AVAudioSessionModeVoiceChat
or AVAudioSessionModeVideoChat
for example), one can get the list of the available input via AVAudioSession's availableInputs
, and those inputs are listed there even when that device is not an active route - this is very useful when a user is doing a manual override via MPVolumeView
from Bluetooth to the speaker, for example, and since HFP is a 2-way IO (has both input and output), you can judge whether output HFP Bluetooth is still available by looking at the inputs.
BOOL isBtInputAvailable = NO;
NSArray *inputs = [[AVAudioSession sharedInstance] availableInputs];
for (AVAudioSessionPortDescription* port in inputs) {
if ([port.portType isEqualToString:AVAudioSessionPortBluetoothHFP]) {
isBtInputAvailable = YES;
break;
}
}
这篇关于检测连接的音频设备 iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!