Tyl*_*ler 7 audio macos cocoa macos-carbon core-audio
所以这是正在发生的事情.
我正在尝试使用Core Audio,特别是输入设备.我想要静音,改变音量等等.我遇到了一些绝对奇怪的东西,我无法弄明白.到目前为止,谷歌一直没有帮助.
当我查询系统并询问所有音频设备的列表时,我返回了一组设备ID.在这种情况下,261,259,263,257.
使用kAudioDevicePropertyDeviceName,我得到以下内容:
261:内置麦克风
259:内置输入
263:内置输出
257:iPhoneSimulatorAudioDevice
这一切都很好.
// 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];
}
Run Code Online (Sandbox Code Playgroud)
当我查询系统并询问它的默认输入设备的ID时,问题就出现了.此方法返回ID为269,未在所有设备的数组中列出.
如果我尝试使用kAudioDevicePropertyDeviceName来获取设备的名称,我将返回一个空字符串.虽然它似乎没有名称,但如果我将此设备ID静音,我的内置麦克风将静音.相反,如果我静音261号,被命名为"内置麦克风",我的麦克风将不会静音.
// 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;
}
Run Code Online (Sandbox Code Playgroud)
为了进一步混淆的东西,如果我手动切换我的输入为"线路",并重新运行该程序,我查询默认输入设备,其中,当得到的259的ID 被所有设备的阵列中列出.
所以,总结一下:
我正在尝试与系统中的输入设备进行交互.如果我尝试与设备ID 261(我的"内置麦克风")进行交互,则不会发生任何事情.如果我尝试与设备ID 269(显然是幻像ID)进行交互,我的内置麦克风会受到影响.当我向系统查询默认输入设备时返回269 ID,但是当我向系统查询所有设备的列表时,它没有列出.
有谁知道发生了什么?我只是疯了吗?
提前致谢!
修复。
首先,虚拟设备 ID 只是系统正在使用的虚拟设备。
其次,我无法对实际设备静音或执行任何操作的原因是因为我使用的是 AudioHardwareServiceSetPropertyData 而不是 AudioObjectSetPropertyData。
现在一切正常。