Skip to main content
 首页 » 编程设计

python之我如何将音量添加到 pyalsaaudio 中的 current_volume 列表

2025年01月19日8jpfss

我正在尝试将音量增加到当前设置的音量,在这种情况下我们会说它是 80%。在 Python 中使用 alsaaudio 模块,有一个名为 getvolume 的函数

#declare alsaaudio 
am = alsaaudio.Mixer() 
#get current volume 
current_volume = am.getvolume() 

getvolume或 current_volume在我的例子中转储了一个列表,例如 [80L] 80% 的音量。我正在尝试像这样将音量增加到当前音量,

#adds 5 on to the current_volume     
increase = current_volume + 5 
am.setvolume(increase) 

但我的问题是,因为它是一个列表,所以我不能删除或替换字符,而且由于我对 Python 比较陌生,所以不知道如何删除列表中的字符,然后在转换后将 5 添加到该整数上。

我在这里创建了一个可运行的例子:

import alsaaudio 
am = alsaaudio.Mixer() 
current_volume = am.getvolume() 
print(repr(current_volume), type(current_volume), type(current_volume[0])) 

它打印: ('[45L]', <type 'list'>, <type 'long'>) ,虽然这个问题已经解决了,但还是感谢您的回复。

请您参考如下方法:

Mixer.getvolume([direction])

返回一个列表,其中包含每个 channel 的当前音量设置。列表元素是整数百分比。

https://www.programcreek.com/python/example/91452/alsaaudio.Mixer

    mixer = alsaaudio.Mixer() 
    value = mixer.getvolume()[0] 
    value = value + 5 
    if value > 100: 
        value = 100 
    mixer.setvolume(value)