【问题标题】:How do you select audio input device in core audio?如何在核心音频中选择音频输入设备?
【发布时间】:2011-03-13 01:38:43
【问题描述】:

我正在编写一个需要处理多个音频输入的程序。

我目前正在使用 AudioQueues 来获取输入,但这只是来自默认输入设备。

有没有办法:

  • 选择 AudioQueue 使用的输入设备。
  • 更改默认输入设备。

我知道我可以使用 Core-Audio 中的 kAudioHardwarePropertyDevices 来获取输出设备列表,是否有类似的可以用于输入设备?

【问题讨论】:

    标签: audio core-audio audiotoolbox


    【解决方案1】:

    kAudioHardwarePropertyDevices 用于输出和输入设备。设备可以同时具有输入和输出通道,也可以只有输入或输出通道。

    大部分 AudioDevice... 函数采用布尔 isInput 参数,以便您可以查询设备的输入端。

    【讨论】:

    • 谢谢!我刚刚发现 kAudioHardwarePropertyDefaultInputDevice 应该可以很好地完成这项工作。不幸的是,AudioDevice 函数已被弃用,所以我必须改用 AudioObjectGetPropertyData,但这没有 isInput 的布尔值。知道如何使用这种方法区分输入和输出设备吗?
    • 看看Tech Note TN2223“Moving Off Deprecated HAL APIs”。我想你会设置AudioObjectPropertyScope 来选择输入或输出。
    • 要区分输入和输出,从AudioStreamID获取AudioStreamID(selector:kAudioDevicePropertyStreams)获取通道方向选择器:kAudioStreamPropertyDirection。方向0表示输出通道,1表示输入通道。
    • @lucius has moved here987654322@提到的技术说明
    【解决方案2】:

    我苦苦思索了好一阵子,终于想通了:

    BOOL isMic = NO;
    BOOL isSpeaker = NO;
    
    AudioDeviceID device        = audioDevices[i];
    
    // Determine direction of the device by asking for the number of input or 
    // output streams.
    propertyAddress.mSelector   = kAudioDevicePropertyStreams;
    propertyAddress.mScope      = kAudioDevicePropertyScopeInput;
    
    UInt32 dataSize             = 0;
    OSStatus status             = AudioObjectGetPropertyDataSize(device, 
                                                                 &propertyAddress, 
                                                                 0, 
                                                                 NULL, 
                                                                 &dataSize);        
    UInt32 streamCount          = dataSize / sizeof(AudioStreamID);
    
    if (streamCount > 0) 
    {
        isMic = YES;
    }
    
    propertyAddress.mScope  = kAudioDevicePropertyScopeOutput;      
    dataSize                = 0;
    status                  = AudioObjectGetPropertyDataSize(device, 
                                                             &propertyAddress, 
                                                             0, 
                                                             NULL,  
                                                             &dataSize);        
    streamCount             = dataSize / sizeof(AudioStreamID);
    
    if (streamCount > 0) 
    {
        isSpeaker = YES;
    }
    

    如您所见,关键部分是使用 ScopeInput/ScopeOutput 参数值。

    【讨论】:

      猜你喜欢
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 2017-09-28
      相关资源
      最近更新 更多