Qt:获取 Linux 中可用的音频设备列表
我想获取 Linux 系统上可用的所有音频设备的列表。然后,我将将此列表显示到组合框中,用户可以从中选择用于录制/播放的设备。根据用户的选择,我将构造 QAudioInput 和 QAudioOutput 用于录制/播放。
根据 Qt 文档,这可以使用 QAudioDeviceInfo::availableDevices 来完成静态方法。此方法返回系统中所有可用音频设备的列表。
我已经使用此方法列出了音频设备的数量。此方法应该返回一个仅包含两个元素的列表,因为我的系统中安装了两个声卡(一个位于主板中,另一个是 USB 设备)。但它返回了一个包含 23 项的列表。当我访问他们的名字时,我得到了类似下面的内容 -
default
pulse
front:CARD=Intel,DEV=0
surround40:CARD=Intel,DEV=0
surround41:CARD=Intel,DEV=0
surround50:CARD=Intel,DEV=0
surround51:CARD=Intel,DEV=0
surround71:CARD=Intel,DEV=0
dmix:CARD=Intel,DEV=0
dsnoop:CARD=Intel,DEV=0
hw:CARD=Intel,DEV=0
plughw:CARD=Intel,DEV=0
front:CARD=default,DEV=0
surround40:CARD=default,DEV=0
surround41:CARD=default,DEV=0
surround50:CARD=default,DEV=0
surround51:CARD=default,DEV=0
surround71:CARD=default,DEV=0
iec958:CARD=default,DEV=0
dmix:CARD=default,DEV=0
dsnoop:CARD=default,DEV=0
hw:CARD=default,DEV=0
plughw:CARD=default,DEV=0
这背后的原因可能是 后端使用的 ALSA API。根据 这篇文章 ALSA 将一个声卡逻辑分区为多个逻辑音频设备,当 Qt API 查询驱动程序时,它会被获取。
我不想列出所有这些名称供用户选择。我想列出类似的内容 -
HDA-Intel
USB-Audio
用户将从中选择他们想要的声卡..
有什么方法可以使用 Qt 来完成此操作吗?如果不是,我真的很想知道这里可以使用哪些其他替代方案。
I want to get the list of all audio devices that are available on my Linux system. Then I will show this list into a combo box from which user will select the device to use for recording/playback. Based on the user's selection, I will then construct QAudioInput and QAudioOutput for recording/playback.
According to the Qt documentation, this can be done using QAudioDeviceInfo::availableDevices static method. This method returns a list of all the available audio devices in my system.
I have used this method to list the number of audio devices. This method should have returned a list containing only two elements since my system has two sound cards installed in it (one is in motherboard, another one is a USB device). But it returned a list containing 23 items. When I accessed their names, I got something like below -
default
pulse
front:CARD=Intel,DEV=0
surround40:CARD=Intel,DEV=0
surround41:CARD=Intel,DEV=0
surround50:CARD=Intel,DEV=0
surround51:CARD=Intel,DEV=0
surround71:CARD=Intel,DEV=0
dmix:CARD=Intel,DEV=0
dsnoop:CARD=Intel,DEV=0
hw:CARD=Intel,DEV=0
plughw:CARD=Intel,DEV=0
front:CARD=default,DEV=0
surround40:CARD=default,DEV=0
surround41:CARD=default,DEV=0
surround50:CARD=default,DEV=0
surround51:CARD=default,DEV=0
surround71:CARD=default,DEV=0
iec958:CARD=default,DEV=0
dmix:CARD=default,DEV=0
dsnoop:CARD=default,DEV=0
hw:CARD=default,DEV=0
plughw:CARD=default,DEV=0
The reason behind this is probably the ALSA API that is being used in the back-end. According to this article ALSA logically partitions a sound card into multiple logical audio devices, which is being fetched by the Qt API when it queries the driver.
I don't want to list all these names for the user to choose from. I want to list something like -
HDA-Intel
USB-Audio
from which users will then choose their desired sound card..
Is there any way this can be done using Qt? If it's not, I would really like to know what other alternatives can be used here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这被称为解决方法,但似乎您通过在列表中列出 CARD= 的不同值来获得此信息:在您的示例中,是英特尔和默认值。
如果您的应用程序应该是跨平台的,那么此命令在其他操作系统下返回什么?
I know it's called a workaround, but it seems you have this information by listing the different values of CARD= in your list : in your example, Intel and default.
If your application is supposed to be cross-platform, what does this command return under other operating systems ?
显然,执行此操作的唯一方法是从 Qt 触发
aplay
/arecord
进程,从该进程获取结果输出并解析输出字符串以查找卡名称和对应的身份证。由于这种方法太依赖于这些进程的输出字符串格式,因此我没有使用它。相反,我选择了 PulseAudio 服务器来获取系统上的可用设备。我还将它用于音频 I/O,因为它提供了比 Qt 多媒体 API 更好的控制。
Apparently the only way to do this is to fire the
aplay
/arecord
process from Qt, get the result output from the process and parse the output string to find card names and corresponding IDs. As this approach is too dependent on the output string format of those processes, I didn't use it.Instead, I chose the PulseAudio server to fetch available devices on my system. I also used it for audio I/O as it provides much better control than Qt's multimedia API.