python-2.7 - 在 Raspberry Pi 上的 Python 程序中更改音量

标签 python-2.7 raspberry-pi raspberry-pi2

我使用 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/

相关文章:

python-2.7 - pygame中的透明矩形

jquery - Raspberry Pi - 使用 JQuery 的 Flask 服务器无法离线工作(在线工作)

python - 使用 Python 模块 SMBus、Raspberry Pi 和 Arduino 时出现输入/输出错误

qt5 - 在 Raspberry Pi 2 上运行简单的 QtWebEngine 应用程序,页面未显示

linux - Raspberry PI USB 网络摄像头 - 罗技 Quickcam 9000

sockets - Raspberry Pi 2服务器不响应

python - 减少大字典使用的内存

python - 在 if 语句中比较三个变量

python - 如何在Python中拆分表中的两列

python - 当 OpenCV 检测到正图像时,处理时间非常长