您可以使用CoreAudio 框架观察变化。
耳机和扬声器都是同一音频输出设备(内置类型)上的数据源。根据是否插入耳机,两者之一将在音频设备上。
要获得通知,您可以在内置输出设备上监听活动数据源的变化。
1。获取内置输出设备
为了简短起见,我们将使用默认输出设备。在大多数情况下,这是内置输出设备。在现实生活中的应用程序中,您需要循环所有可用设备来找到它,因为默认设备可以设置为不同的音频设备(例如 soundflower 或 airplay)。
AudioDeviceID defaultDevice = 0;
UInt32 defaultSize = sizeof(AudioDeviceID);
const AudioObjectPropertyAddress defaultAddr = {
kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
AudioObjectGetPropertyData(kAudioObjectSystemObject, &defaultAddr, 0, NULL, &defaultSize, &defaultDevice);
2。读取其当前数据源
设备上的当前数据源由 UInt32 类型的 ID 标识。
AudioObjectPropertyAddress sourceAddr;
sourceAddr.mSelector = kAudioDevicePropertyDataSource;
sourceAddr.mScope = kAudioDevicePropertyScopeOutput;
sourceAddr.mElement = kAudioObjectPropertyElementMaster;
UInt32 dataSourceId = 0;
UInt32 dataSourceIdSize = sizeof(UInt32);
AudioObjectGetPropertyData(defaultDevice, &sourceAddr, 0, NULL, &dataSourceIdSize, &dataSourceId);
3。观察数据源的变化
AudioObjectAddPropertyListenerBlock(_defaultDevice, &sourceAddr, dispatch_get_current_queue(), ^(UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses) {
// move to step 2. to read the updated value
});
确定数据源类型
当您的数据源 ID 为 UInt32 时,您可以使用值转换器查询音频对象的属性。例如,要将源名称作为字符串使用kAudioDevicePropertyDataSourceNameForIDCFString。这将产生字符串“内部扬声器”或“耳机”。但是,这可能因用户区域而异。
更简单的方法是直接比较数据源id:
if (dataSourceId == 'ispk') {
// Recognized as internal speakers
} else if (dataSourceId == 'hdpn') {
// Recognized as headphones
}
但是我找不到为这些值定义的任何常量,所以这有点无证。