枚举HAL AudioID的CoreAudio不一致
我正在进行正常的“迭代”过程以获取Cocoa上可用音频设备的列表,并且遇到了一个奇怪的情况。 这是一段代码片段:
AudioObjectPropertyAddress propertyAddress =
{
kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
UInt32 dataSize = 0;
OSStatus status = AudioHardwareServiceGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize);
if(status != kAudioHardwareNoError)
{
NSLog (@"AudioObjectGetPropertyDataSize (kAudioHardwarePropertyDevices) failed: %i", status);
return;
}
UInt32 deviceCount = (UInt32)(dataSize / sizeof(AudioDeviceID));
AudioDeviceID *audioDevices = (AudioDeviceID *)(calloc(dataSize,1));
if(NULL == audioDevices)
{
NSLog (@"Unable to allocate memory");
return;
}
status = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize, audioDevices);
if(status != kAudioHardwareNoError)
{
fprintf(stderr, "AudioObjectGetPropertyData (kAudioHardwarePropertyDevices) failed: %in", status);
free(audioDevices);
audioDevices = NULL;
return;
}
CFMutableArrayRef inputDeviceArray = CFArrayCreateMutable(kCFAllocatorDefault, deviceCount, &kCFTypeArrayCallBacks);
if(NULL == inputDeviceArray)
{
NSLog (@"CFArrayCreateMutable failed");
free(audioDevices);
audioDevices = NULL;
return;
}
// Iterate through all the devices and determine which are input-capable
propertyAddress.mScope = kAudioDevicePropertyScopeInput;
for(UInt32 i = 0; i < deviceCount; ++i)
{
// Query device UID
CFStringRef deviceUID = NULL;
dataSize = sizeof(deviceUID);
propertyAddress.mSelector = kAudioDevicePropertyDeviceUID;
status = AudioHardwareServiceGetPropertyData(audioDevices[i], &propertyAddress, 0, NULL, &dataSize, &deviceUID);
if(status != kAudioHardwareNoError)
{
NSLog (@"AudioObjectGetPropertyData (kAudioDevicePropertyDeviceUID) failed: %i", status);
continue;
}
// For fun, let's do a reverse lookup for device ID
AudioDeviceID ourDeviceID;
UInt32 dataSize = sizeof(ourDeviceID);
propertyAddress.mSelector = kAudioHardwarePropertyTranslateUIDToDevice;
// Just for fun, get the default input device and compare results
status = AudioObjectGetPropertyData(kAudioObjectSystemObject,
&propertyAddress,
sizeof(deviceUID),
&deviceUID,
&dataSize,
&ourDeviceID);
if (kAudioHardwareNoError != status)
{
NSLog (@"Can't reverse lookup audio id");
}
if (ourDeviceID != audioDevices[i])
NSLog (@"Reversed audio id %d Not equal to audioDevice[] of %d", ourDeviceID, audioDevices[i]);
我这样做是为了获得硬件设备UID列表,以便我可以选择它们供以后使用。
奇怪的是,在运行El Capitan的旧MacPro上,输入数组中的音频设备ID与'kAudioHardwarePropertyTranslateUIDToDevice'调用返回的内容不匹配。 在其他系统上,ID确实匹配。
我的底部NSLog函数吐出这样的信息:
Reversed audio id 64 Not equal to audioDevice[] of 68
Reversed audio id 92 Not equal to audioDevice[] of 96
Reversed audio id 85 Not equal to audioDevice[] of 89
Reversed audio id 76 Not equal to audioDevice[] of 80
Reversed audio id 72 Not equal to audioDevice[] of 76
Reversed audio id 56 Not equal to audioDevice[] of 60
Reversed audio id 48 Not equal to audioDevice[] of 52
Reversed audio id 99 Not equal to audioDevice[] of 103
Reversed audio id 96 Not equal to audioDevice[] of 100
这是为我的系统提供的9个音频设备。 这在尝试为我的HAL输出设备选择AudioID时造成了很大的问题,但自从转换为'kAudioHardwarePropertyTranslateUIDToDevice'调用后,我似乎已经解决了这个问题。
真正的问题是:为什么这些数字会不同? 他们不应该总是返回相同的号码吗?
链接地址: http://www.djcxy.com/p/60187.html