核心音频和 Phantom 设备 ID

发布于 2024-12-28 03:12:28 字数 3102 浏览 1 评论 0原文

所以这就是发生的事情。

我正在尝试使用 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,但当我向系统查询所有设备列表时,不会列出该ID。

有谁知道发生了什么事?我是不是简直要疯了?

提前致谢!

So here's what is going on.

I am attempting to work with Core Audio, specifically input devices. I want to mute, change volume, etc, etc. I've encountered something absolutely bizarre that I cannot figure out. Thus far, google has been of no help.

When I query the system and ask for a list of all audio devices, I am returned an array of device IDs. In this case, 261, 259, 263, 257.

Using kAudioDevicePropertyDeviceName, I get the following:

261: Built-in Microphone
259: Built-in Input
263: Built-in Output
257: iPhoneSimulatorAudioDevice

This is all well and good.

// 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];
}

The problem crops up when I then query the system and ask it for the ID of the default input device. This method returns an ID of 269, which is not listed in the array of all devices.

If I attempt to use kAudioDevicePropertyDeviceName to get the name of the device, I am returned an empty string. Although it doesn't appear to have a name, if I mute this device ID, my built-in microphone will mute. Conversely, if I mute the 261 ID, which is named "Built-In Microphone", my microphone does not mute.

// 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;
}

To further confuse things, if I manually switch my input to "Line In" and re-run the program, I get an ID of 259 when querying for the default input device, which is listed in the array of all devices.

So, to summarize:

I am attempting to interact with the input devices in my system. If I try to interact with device ID 261 which is my "Built-In Microphone", nothing happens. If I try to interact with device ID 269 which is, apparently, a phantom ID, my built-in microphone is affected. The 269 ID is returned when I query the system for the default input device, but it is not listed when I query the system for a list of all devices.

Does anyone know what is happening? Am I simply going insane?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻了热茶 2025-01-04 03:12:28

修好了。

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

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

现在一切正常了。

Fixed it.

First off, the phantom device ID was simply a virtual device the system was using.

Secondly, the reason I couldn't mute or do anything with the actual devices was because I was using AudioHardwareServiceSetPropertyData instead of AudioObjectSetPropertyData.

It all works now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文