【问题标题】:How to set speaker configuration programatically using Core Audio API on Mac OS X?如何在 Mac OS X 上使用 Core Audio API 以编程方式设置扬声器配置?
【发布时间】:2013-05-15 05:14:24
【问题描述】:

我有一个 7.1 声道音频输出设备和一个自定义 kext 来驱动它。我的自定义应用程序需要向设备发送 7.1 后声道音频数据,但设备仅接收 2 声道音频数据。我检查了“音频 MIDI 设置”应用程序中的“配置扬声器”选项,它设置为立体声。当我将其设置为“7.1 后环绕”时,一切正常。在我的最终产品中,我不希望用户必须手动完成所有这些操作。所以,问题是 - 是否有任何 Core Audio API 或任何其他以编程方式执行此操作的方法?

【问题讨论】:

    标签: macos core-audio iokit kernel-extension


    【解决方案1】:

    好的,在玩了一些 Core Audio API 之后,我终于可以完成了。

    1. 获取 AudioDeviceID:

      AudioDeviceID audioDevice = getMyAwesomeDeviceID();
      
    2. 创建一个 AudioObjectPropertyAddress:

      AudioObjectPropertyAddress propertyAddress;
      propertyAddress.mSelector = kAudioDevicePropertyPreferredChannelLayout;
      propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
      propertyAddress.mElement = kAudioObjectPropertyElementMaster;
      
    3. 查询音频对象是否有这个属性:

      AudioObjectHasProperty(audioDevice, &propertyAddress)
      
    4. 获取该属性的数据大小并创建AudioChannelLayout:

      UInt32 propSize(0);
      AudioObjectGetPropertyDataSize(audioDevice, &propertyAddress, 0, NULL, &propSize);
      AudioChannelLayout* layout = (AudioChannelLayout*)malloc(propSize);
      
    5. 配置您的 AudioChannelLayout 结构(例如:立体声布局):

      AudioChannelLabel labels[2] = {kAudioChannelLabel_Right, kAudioChannelLabel_Left};
      
      layout->mNumberChannelDescriptions = 2;
      for (UInt32 i = 2; i < layout->mNumberChannelDescriptions; i++) {
          layout->mChannelDescriptions[i].mChannelLabel = labels[i];
          layout->mChannelDescriptions[i].mChannelFlags = kAudioChannelFlags_AllOff;
      }
      
    6. 设置AudioObject属性数据:

      AudioObjectSetPropertyData(audioDevice, &propertyAddress, 0, NULL, propSize, layout);
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多