我使用 Raspbery Pi B+ 2。我有一个 Python 程序,它使用超声波传感器测量到物体的距离。我想要的是根据与人的距离改变音量。有一个 Python 代码来获取距离,我不知道如何通过 Python 代码更改 Raspbery Pi 音量。
有什么办法吗?
最佳答案
您可以使用包 python-alsaaudio。 安装和使用非常简单。
安装运行:
sudo apt-get install python-alsaaudio
在您的 Python 脚本中,导入模块:
import alsaaudio
现在,您需要获取主调音台并获取/设置音量:
m = alsaaudio.Mixer()
current_volume = m.getvolume() # Get the current Volume
m.setvolume(70) # Set the volume to 70%.
如果 m = alsaaudio.Mixer()
行抛出错误,则尝试:
m = alsaaudio.Mixer('PCM')
这可能是因为 Pi 使用 PCM 而不是主 channel 。
您可以通过运行命令 amixer
查看有关 Pi 的音频 channel 、音量(等)的更多信息。
关于python-2.7 - 在 Raspberry Pi 上的 Python 程序中更改音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41592431/