【问题标题】:Core Audio and the Phantom Device IDCore Audio 和 Phantom 设备 ID
【发布时间】:2012-02-14 17:23:41
【问题描述】:

所以这就是发生的事情。

我正在尝试使用 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];
}

当我查询系统并询问默认输入设备的 ID 时,问题就出现了。此方法返回的 ID 为 269,它列在所有设备的数组中。

如果我尝试使用 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,但在向系统查询所有设备列表时未列出。

有人知道发生了什么吗?我是不是快疯了?

提前致谢!

【问题讨论】:

    标签: macos cocoa audio core-audio macos-carbon


    【解决方案1】:

    修复它。

    首先,虚拟设备 ID 只是系统正在使用的虚拟设备。

    其次,我无法静音或对实际设备执行任何操作的原因是因为我使用的是 AudioHardwareServiceSetPropertyData 而不是 AudioObjectSetPropertyData。

    现在一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多