在 Raspberry Pi 上的 Python 程序中更改音量
Changing volume in Python program on Raspbery Pi
我使用 Raspbery Pi B+ 2。我有一个 Python 程序使用超声波传感器测量到物体的距离。我想要的是根据与人的距离改变音量。有一个 Python 代码来获取距离,我不知道如何通过 Python.
中的代码更改 Raspbery Pi 音量
有什么办法吗?
您可以使用包 python-alsaaudio。
安装使用非常简单
安装运行:
sudo apt-get install python-alsaaudio
在您的 Python 脚本中,导入模块:
import alsaaudio
现在,您需要获得主调音台和get/set音量:
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 而不是主通道。
您可以通过 运行 命令 amixer
.
查看有关 Pi 音频通道、音量(等)的更多信息
我为两个按钮的音量控制制作了一个简单的 python 服务。基于@ant0nisk 提出的内容。
https://gist.github.com/peteristhegreat/3c94963d5b3a876b27accf86d0a7f7c0
它显示获取和设置音量,以及静音。
- 正在收集可用的实际混音器(将提供可用卡的列表):
import alsaaudio as audio
scanCards = audio.cards()
print("cards:", scanCards)
就我而言,我有以下列表:
[u'PCH', u'headset']
- 每张卡片扫描混音器:
for card in scanCards:
scanMixers = audio.mixers(scanCards.index(card))
print("mixers:", scanMixers)
就我而言,我有以下两个列表:
[u'Master', u'Headphone', u'Speaker', u'PCM', u'Mic', u'Mic Boost', u'IEC958', u'IEC958', u'IEC958', u'IEC958', u'IEC958', u'Beep', u'Capture', u'Auto-Mute Mode', u'Internal Mic Boost', u'Loopback Mixing']
[u'Headphone', u'Mic', u'Auto Gain Control']
如您所见,"Master" 并不总是可用的混音器,但传统上期望主混音器的等效项位于索引 0。(并不意味着总是!)
- 在这种情况下,要控制 USB 耳机的音量将遵循以下步骤。
调高音量
def volumeMasterUP():
mixer = audio.Mixer('Headphone', cardindex=1)
volume = mixer.getvolume()
newVolume = int(volume[0])+10
if newVolume <= 100:
mixer.setvolume(newVolume)
调低音量
def volumeMasterDOWN():
mixer = audio.Mixer('Headphone', cardindex=1)
volume = mixer.getvolume()
newVolume = int(volume[0])-10
if newVolume >= 0:
mixer.setvolume(newVolume)
我使用 Raspbery Pi B+ 2。我有一个 Python 程序使用超声波传感器测量到物体的距离。我想要的是根据与人的距离改变音量。有一个 Python 代码来获取距离,我不知道如何通过 Python.
中的代码更改 Raspbery Pi 音量有什么办法吗?
您可以使用包 python-alsaaudio。 安装使用非常简单
安装运行:
sudo apt-get install python-alsaaudio
在您的 Python 脚本中,导入模块:
import alsaaudio
现在,您需要获得主调音台和get/set音量:
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 而不是主通道。
您可以通过 运行 命令 amixer
.
我为两个按钮的音量控制制作了一个简单的 python 服务。基于@ant0nisk 提出的内容。
https://gist.github.com/peteristhegreat/3c94963d5b3a876b27accf86d0a7f7c0
它显示获取和设置音量,以及静音。
- 正在收集可用的实际混音器(将提供可用卡的列表):
import alsaaudio as audio
scanCards = audio.cards()
print("cards:", scanCards)
就我而言,我有以下列表:
[u'PCH', u'headset']
- 每张卡片扫描混音器:
for card in scanCards:
scanMixers = audio.mixers(scanCards.index(card))
print("mixers:", scanMixers)
就我而言,我有以下两个列表:
[u'Master', u'Headphone', u'Speaker', u'PCM', u'Mic', u'Mic Boost', u'IEC958', u'IEC958', u'IEC958', u'IEC958', u'IEC958', u'Beep', u'Capture', u'Auto-Mute Mode', u'Internal Mic Boost', u'Loopback Mixing']
[u'Headphone', u'Mic', u'Auto Gain Control']
如您所见,"Master" 并不总是可用的混音器,但传统上期望主混音器的等效项位于索引 0。(并不意味着总是!)
- 在这种情况下,要控制 USB 耳机的音量将遵循以下步骤。
调高音量
def volumeMasterUP():
mixer = audio.Mixer('Headphone', cardindex=1)
volume = mixer.getvolume()
newVolume = int(volume[0])+10
if newVolume <= 100:
mixer.setvolume(newVolume)
调低音量
def volumeMasterDOWN():
mixer = audio.Mixer('Headphone', cardindex=1)
volume = mixer.getvolume()
newVolume = int(volume[0])-10
if newVolume >= 0:
mixer.setvolume(newVolume)