这就是发生的事情。
我正在尝试使用 Core Audio,特别是输入设备。我想静音、改变音量等等。我遇到了一些我无法弄清楚的绝对奇怪的事情。到目前为止,谷歌还没有提供任何帮助。
当我查询系统并请求所有音频设备的列表时,我会返回一个设备 ID 数组。在本例中为 261、259、263、257。
使用 kAudioDevicePropertyDeviceName,我得到以下信息:
261:内置麦克风
259:内置输入
263:内置输出
257:iPhone模拟器音频设备
这一切都很好。
// This method returns an NSArray of all the audio devices on the system, both input and
// On my system, it returns 261, 259, 263, 257
- (NSArray*)getAudioDevices
{
AudioObjectPropertyAddress propertyAddress = {
kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
UInt32 dataSize = 0;
OSStatus status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize);
if(kAudioHardwareNoError != status)
{
MZLog(@"Unable to get number of audio devices. Error: %d",status);
return NULL;
}
UInt32 deviceCount = dataSize / sizeof(AudioDeviceID);
AudioDeviceID *audioDevices = malloc(dataSize);
status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize, audioDevices);
if(kAudioHardwareNoError != status)
{
MZLog(@"AudioObjectGetPropertyData failed when getting device IDs. Error: %d",status);
free(audioDevices), audioDevices = NULL;
return NULL;
}
NSMutableArray* devices = [NSMutableArray array];
for(UInt32 i = 0; i < deviceCount; i++)
{
MZLog(@"device found: %d",audioDevices[i]);
[devices addObject:[NSNumber numberWithInt:audioDevices[i]]];
}
free(audioDevices);
return [NSArray arrayWithArray:devices];
}
当我查询系统并询问默认输入设备的 ID 时,问题就出现了。此方法返回 ID 269,该 ID 未列在所有设备的数组中。
如果我尝试使用 kAudioDevicePropertyDeviceName 来获取设备名称,则会返回一个空字符串。虽然它看起来没有名称,但如果我将此设备 ID 静音,我的内置麦克风也会静音。相反,如果我将名为“内置麦克风”的 261 ID 静音,我的麦克风不会静音。
// Gets the current default audio input device
// On my system, it returns 269, which is NOT LISTED in the array of ALL audio devices
- (AudioDeviceID)defaultInputDevice
{
AudioDeviceID defaultAudioDevice;
UInt32 propertySize = 0;
OSStatus status = noErr;
AudioObjectPropertyAddress propertyAOPA;
propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
propertyAOPA.mScope = kAudioObjectPropertyScopeGlobal;
propertyAOPA.mSelector = kAudioHardwarePropertyDefaultInputDevice;
propertySize = sizeof(AudioDeviceID);
status = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject, &propertyAOPA, 0, NULL, &propertySize, &defaultAudioDevice);
if(status)
{ //Error
NSLog(@"Error %d retreiving default input device",status);
return 0;
}
return defaultAudioDevice;
}
更令人困惑的是,如果我手动将输入切换为“Line In”并重新运行程序,则在查询默认输入设备时会得到 ID 259,该设备已列出在所有设备的数组中。
所以,总结一下:
我正在尝试与系统中的输入设备进行交互。如果我尝试与设备 ID 261(即我的“内置麦克风”)进行交互,则不会发生任何情况。如果我尝试与设备 ID 269(显然是幻像 ID)进行交互,我的内置麦克风就会受到影响。向系统查询默认输入设备时返回269 ID,但向系统查询所有设备列表时未列出该ID。
有谁知道发生了什么事吗?我是不是疯了?
提前致谢!
最佳答案
已修复。
首先,幻像设备 ID 只是系统正在使用的虚拟设备。
其次,我无法静音或对实际设备执行任何操作的原因是因为我使用的是 AudioHardwareServiceSetPropertyData 而不是 AudioObjectSetPropertyData。
现在一切正常。
关于macos - 核心音频和 Phantom 设备 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8936434/