如何用python 3控制树莓派的音量? [英] How to control the volume of raspberry pi with python 3?

查看:98
本文介绍了如何用python 3控制树莓派的音量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上查找有关使用 python 脚本控制 raspberry pi (b+) 音量的任何参考资料.我想出了这个线程之前问过但是python-alsaaudio不适用于 python 3 或在 thonny python idle 中说.因此,我需要知道根据用户输入更改 pi 音量的任何正确方法.

I have been looking up the web for any reference to control the volume of raspberry pi (b+) with python script . I come up with this thread previously asked but python-alsaaudio doesn't works with python 3 or say in the thonny python idle . So I need to know any correct way to change the volume of pi as per the user input .

推荐答案

另一种方式是通过命令行工具控制音量.有一个 Alsa 命令行工具叫做 amixer:

Another way is to control volume through a command line tool. There is a tool for Alsa command line called amixer:

amixer sset Master 50%

现在你可以创建一个简单的python脚本来运行上面的命令:

Now you can create a simple python script that runs the above command:

import subprocess


# a value between 0 and 100
volume = 50
command = ["amixer", "sset", "Master", "{}%".format(volume)]
subprocess.Popen(command)

您可以将 Master 更改为其他声卡.您可以获得控件列表:

You can change Master to other soundcards. You can get a list of controls:

$ amixer scontrols

Simple mixer control 'Master',0
Simple mixer control 'PCM',0
Simple mixer control 'Line',0
Simple mixer control 'CD',0
Simple mixer control 'Mic',0
Simple mixer control 'Mic Boost (+20dB)',0
Simple mixer control 'Video',0
Simple mixer control 'Phone',0
Simple mixer control 'IEC958',0
Simple mixer control 'Aux',0
Simple mixer control 'Capture',0
Simple mixer control 'Mix',0
Simple mixer control 'Mix Mono',0

这篇关于如何用python 3控制树莓派的音量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆