我正在试图弄清楚如何在iphone / ipad / ipod上连接哪个音频设备 . 我知道有关音频路由呼叫和路由更改回调的所有内容,但这些并没有告诉我任何关于附加内容的信息 . 它们仅报告音频当前路由的位置 . 例如,我需要知道,当音频通过扬声器路由时,耳机和/或蓝牙是否仍然连接 . 或者,例如,如果用户在使用蓝牙时插入耳机然后决定断开蓝牙,我需要知道即使音频仍然通过耳机路由,蓝牙也会断开连接 .
对于iOS 5,您应该使用:
CFStringRef newRoute; size = sizeof(CFStringRef); XThrowIfError(AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &newRoute), "couldn't get new audio route"); if (newRoute) { CFShow(newRoute); if (CFStringCompare(newRoute, CFSTR("HeadsetInOut"), NULL) == kCFCompareEqualTo) // headset plugged in { colorLevels[0] = .3; colorLevels[5] = .5; } else if (CFStringCompare(newRoute, CFSTR("SpeakerAndMicrophone"), NULL) == kCFCompareEqualTo) }
您可以从AudioSession属性获取InputSources和OutputDestinations的列表 . 查看这些会话属性:
kAudioSessionProperty_InputSources kAudioSessionProperty_OutputDestinations
并查询每个细节,您可以使用:
kAudioSessionProperty_InputSource kAudioSessionProperty_OutputDestination
不幸的是,从iOS11开始,它似乎有'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 ,即使仍然可以连接多个设备 .
currentRoute.outputs
但是,对于输入,包括具有HFP配置文件的蓝牙设备,如果使用正确的音频会话模式(例如 AVAudioSessionModeVoiceChat 或 AVAudioSessionModeVideoChat ),可以通过AVAudioSession的 availableInputs 获取可用输入的列表,并在那里列出那些输入即使该设备不是活动路由 - 例如,当用户通过 MPVolumeView 从蓝牙到扬声器进行手动覆盖时,这非常有用,并且由于HFP是双向IO(具有输入和输出),您可以通过查看输入来判断输出HFP蓝牙是否仍然可用 .
AVAudioSessionModeVoiceChat
AVAudioSessionModeVideoChat
availableInputs
MPVolumeView
BOOL isBtInputAvailable = NO; NSArray *inputs = [[AVAudioSession sharedInstance] availableInputs]; for (AVAudioSessionPortDescription* port in inputs) { if ([port.portType isEqualToString:AVAudioSessionPortBluetoothHFP]) { isBtInputAvailable = YES; break; } }
3 回答
对于iOS 5,您应该使用:
您可以从AudioSession属性获取InputSources和OutputDestinations的列表 . 查看这些会话属性:
并查询每个细节,您可以使用:
不幸的是,从iOS11开始,它似乎有'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
,即使仍然可以连接多个设备 .但是,对于输入,包括具有HFP配置文件的蓝牙设备,如果使用正确的音频会话模式(例如
AVAudioSessionModeVoiceChat
或AVAudioSessionModeVideoChat
),可以通过AVAudioSession的availableInputs
获取可用输入的列表,并在那里列出那些输入即使该设备不是活动路由 - 例如,当用户通过MPVolumeView
从蓝牙到扬声器进行手动覆盖时,这非常有用,并且由于HFP是双向IO(具有输入和输出),您可以通过查看输入来判断输出HFP蓝牙是否仍然可用 .