python - 通过 Python 选择要播放的音频设备

标签 python linux python-3.x audio

如何在 linux 中获取音频设备列表(如 aplay -L)并为输出选择音频设备?有没有要导入的库?他们已经进口了吗?

最佳答案

在编写声音均衡器时,我到处搜索你的要求,但从未找到满意的答案。
有一个可用的 alsaaudio 库:

import alsaaudio   #alternative method pure python
for device_number, card_name in enumerate(alsaaudio.cards()):
    print device_number, card_name

我最终将 pacmdcat 选项添加到命令行,检索并解析结果以发现什么是什么。
示例:

    sink_list = os.popen('cat /proc/asound/cards | grep "\[" | cut -c2').readlines()
    for s in sink_list:
        s = s.strip()
        self.sinks_available.append("hw:"+s)

对于 pulseaudio 你需要 pacmd

# Identify the default sound card device
    sink = os.popen('pacmd list | grep "device.master_device" | cut --delimiter== -f2').readline()
    if sink =="":
        sink = os.popen('pacmd list-cards | grep -C1 sinks | grep pci | cut --delimiter=/ -f1').readline()

这一切都是最不令人满意的,而且不像 python 一样,但当魔鬼开车时必须这样做

关于python - 通过 Python 选择要播放的音频设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39147723/

相关文章:

python - 使用 hist2d 在 matplotlib 中创建对数线性图

linux - 如何在 yocto 中禁用 psplash 屏幕

python-3.x - 仅从 Python 中的大文件中读取特定行号?

python - 列上的十分位数 Pandas DataFrame

python - 给定空间中的三个点(3D)找到弧/圆方程

python - 适用于 Python 的 Amazon SQS 的替代方案

Python:在随机段落中找到最长/最短的句子?

linux - 如何在kshell中获取前几天的日期

linux - SMTP 用户未知

python - 如何在Python中解析和打印树