如何在核心音频中选择音频输入设备?

6
我正在编写一个需要处理多个音频输入的程序。
目前,我正在使用AudioQueues来获取输入,但这只是从默认输入设备获取。
有没有办法:
- 选择AudioQueues使用哪个输入设备。 - 更改默认输入设备。
我知道可以在Core-Audio中使用kAudioHardwarePropertyDevices获取输出设备列表,是否有类似的用于输入设备的属性?
2个回答

3

我曾经困惑了很长时间,不知道如何处理这个问题,最终找到了解决方法:

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参数值。


2

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

大多数的AudioDevice...函数都带有一个布尔型的isInput参数,以便您可以查询设备的输入端。


1
谢谢!我刚找到了kAudioHardwarePropertyDefaultInputDevice,这个方法应该很好用。不幸的是,AudioDevice函数已经被废弃,所以我必须使用AudioObjectGetPropertyData。但是这个方法没有一个布尔值来表示输入设备或输出设备。你有什么办法可以用这个方法区分输入和输出设备吗? - DanielGibbs
1
请查看Tech Note TN2223 "Moving Off Deprecated HAL APIs"。我认为您需要设置AudioObjectPropertyScope来选择输入或输出。 - lucius
2
为了区分输入和输出,可以从AudioStreamID获取AudioStreamID(selector:kAudioDevicePropertyStreams),然后根据通道方向选择器 kAudioStreamPropertyDirection 来获取通道方向。方向0表示输出通道,1表示输入通道。 - Raviprakash
@lucius提到的技术笔记已经移至此处:https://developer.apple.com/library/archive/technotes/tn2223/_index.html - Grumdrig

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接