objective-c - 无法从 cocoa Objective-C 的任何音频设备获取输出音量

标签 objective-c cocoa audio core-audio

我正在尝试在 macOS 应用程序中实现音量控制。当我运行循环遍历 MacBook Pro 中安装的音频设备并查询其主音量(或单个 channel 音量 - 我也尝试过)的代码时,我无法得到任何结果。

这是我正在运行的代码:

- (NSArray*)getAudioDevices
{
  AudioObjectPropertyAddress propertyAddress = { 
    kAudioHardwarePropertyDevices, 
    kAudioObjectPropertyScopeGlobal, 
    kAudioObjectPropertyElementMaster 
  };

  UInt32 dataSize = 0;
  OSStatus status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize);
  if(kAudioHardwareNoError != status)
  {
    NSLog(@"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) 
  {
    NSLog(@"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++)
  {    

    // Query device name
    CFStringRef deviceName = NULL;
    dataSize = sizeof(deviceName);
    propertyAddress.mSelector = kAudioDevicePropertyDeviceNameCFString;
    status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, NULL, &dataSize, &deviceName);
    if(kAudioHardwareNoError != status) {
      fprintf(stderr, "AudioObjectGetPropertyData (kAudioDevicePropertyDeviceNameCFString) failed: %i\n", status);
      continue;
    }

    // Query device output volume
    Float32 volume;
    propertyAddress.mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume;
    status = AudioHardwareServiceHasProperty(audioDevices[i], &propertyAddress);
    if(status) {
      fprintf(stderr, "AudioObjectGetPropertyData (kAudioHardwareServiceDeviceProperty_VirtualMasterVolume) failed: %i\n", status);
    } else {
      dataSize = sizeof(volume);
      status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, NULL, &dataSize, &volume);
      if (status) {
        // handle error
      }
    }

    NSLog(@"device found: %d - %@ || Vol: %f || status %i",audioDevices[i], deviceName, volume, status);  

    [devices addObject:[NSNumber numberWithInt:audioDevices[i]]];
  }

  free(audioDevices);

  return [NSArray arrayWithArray:devices];
}

以及我从日志中获得的输出:

device found: 58 - Built-in Microphone || Vol: 0.000000 || status 2003332927
device found: 78 - Built-in Input || Vol: 0.000000 || status 2003332927
device found: 68 - Built-in Output || Vol: 0.000000 || status 2003332927
device found: 54 - Microsoft LifeCam VX-5000 || Vol: 0.000000 || status 2003332927
device found: 87 - Microsoft LifeChat LX-3000  || Vol: 0.000000 || status 2003332927

我正在运行 OS X 10.7.3/Xcode 4.3.2

设备上的音量实际上并未设置为零。谁能告诉我为什么我无法获得任何值?

最佳答案

我不确定仅通过更改 mSelector 即可正确形成您的 propertyAddress。尝试使用以下值创建一个新的 propertyAddress:

AudioObjectPropertyAddress propertyAddress = { 
    kAudioDevicePropertyVolumeScalar, 
    kAudioDevicePropertyScopeOutput,
    1 
};

并在最终的 AudioObjectGetPropertyData 调用中使用它。

关于objective-c - 无法从 cocoa Objective-C 的任何音频设备获取输出音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9998839/

相关文章:

ios - Objective-C 中是否有类似于 Java 中的 Netty 的机制,用于在传输层将 TCP 转移到 UDT 协议(protocol)

iOS 分段上传照片

objective-c - NSString 之间的区别?

objective-c - 在 Mac 上创建自定义文件扩展名

ios - 如何获得行数?

cocoa - 自定义绘图顶部的 NSTextField - 黑色轮廓和光标不闪烁?

javascript - 如何捕获 HTML5 音频中的 'stop' 事件然后执行某些操作?

javascript - 我只想创建一个音频播放器实例

firefox - Firefox播放Ogg

objective-c - 当我调用 `-copy` 时,为什么我的 NSImage 的 name 属性没有被复制?