【问题标题】:Get Built-in Output From Core Audio从 Core Audio 获取内置输出
【发布时间】:2012-07-05 15:48:27
【问题描述】:

是否有可能可靠地获得 Mac 内置输出的AudioDeviceID?我尝试使用kAudioHardwarePropertyDefaultOutputDevice 来获取当前选择的输出设备,但这可以是任何旧设备,具体取决于用户在系统偏好设置中选择的内容——我只想要内置扬声器的 ID。

AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyDefaultInputDevice,
                                          kAudioObjectPropertyScopeGlobal,
                                          kAudioObjectPropertyElementMaster };

verify_noerr (AudioObjectGetPropertyData(kAudioObjectSystemObject,
                                         &theAddress,
                                         0,
                                         NULL,
                                         &propsize,
                                         &inputDevice));

目前我正在获取所有设备的列表并通过执行name == "Built-in Output" 检查设备的名称字符串。这适用于我的机器,但似乎不是一个非常强大的解决方案!有没有类似kAudioHardwarePropertyBuiltInOutputDevice 的东西?

【问题讨论】:

    标签: objective-c macos core-audio


    【解决方案1】:

    @Equinox2000 的答案是有效的,但有一种方法可以避免繁重的字符串比较。只需使用kAudioDevicePropertyTransportType 的选择器获取另一个AudioObject 的属性。

    aopa.mSelector = kAudioDevicePropertyTransportType;
    aopa.mScope = kAudioObjectPropertyScopeGlobal;
    UInt32 size = sizeof(UInt32);
    UInt32 transportType = 0;
    OSStatus status = AudioObjectGetPropertyData(device.deviceId, &address, 0, NULL, &size, & transportType);
    if (transportType == kAudioDeviceTransportTypeBuiltIn) // that's all the checks
        // do something
    

    【讨论】:

      【解决方案2】:

      经过几天的研究,我能想出的最佳答案是寻找您提到的内置输出,但还添加了制造商检查,如下所示...

      - (NSString *)getBuiltInOutputDeviceUID
      {
          NSString *deviceUID = @"";
      
          AudioObjectPropertyAddress aopa;
          aopa.mSelector = kAudioHardwarePropertyDevices;
          aopa.mScope = kAudioObjectPropertyScopeGlobal;
          aopa.mElement = kAudioObjectPropertyElementMaster;
      
          UInt32 propSize;
          OSStatus error = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &aopa, 0, NULL, &propSize);
          if (error == noErr) {
              int deviceCount = propSize / sizeof(AudioDeviceID);
              AudioDeviceID *audioDevices = (AudioDeviceID *)malloc(propSize);
              error = AudioObjectGetPropertyData(kAudioObjectSystemObject, &aopa, 0, NULL, &propSize, audioDevices);
              if (error == noErr) {
                  UInt32 propSize = sizeof(CFStringRef);
                  for(int i = 1; i <= deviceCount; i++) {
                      NSString *result;
                      aopa.mSelector = kAudioDevicePropertyDeviceManufacturerCFString;
                      error = AudioObjectGetPropertyData(audioDevices[i], &aopa, 0, NULL, &propSize, &result);
                      if (error != noErr || ![result isEqualToString:@"Apple Inc."]) {
                          continue;
                      }
                      aopa.mSelector = kAudioDevicePropertyDeviceNameCFString;
                      error = AudioObjectGetPropertyData(audioDevices[i], &aopa, 0, NULL, &propSize, &result);
                      if (error != noErr || ![result isEqualToString:@"Built-in Output"]) {
                          continue;
                      }
                      aopa.mSelector = kAudioDevicePropertyDeviceUID;
                      error = AudioObjectGetPropertyData(audioDevices[i], &aopa, 0, NULL, &propSize, &result);
                      if (error == noErr) {
                          deviceUID = result;
                          break;
                      }
                  }
              }
              free(audioDevices);
          }
          return deviceUID;
      }
      

      【讨论】:

      • 我们在您的代码中发现了一个错误,在获取您需要的设备列表时生成错误,如果没有生成错误的设备并且另一个跳转 for(int i = 0; i
      猜你喜欢
      • 2014-01-27
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      相关资源
      最近更新 更多