I can control volume using this command through terminal amixer -D pulse sset Master 0% . My question that how can I do the same thing using python script.
3 Answers
You can use call from the subprocess module:
from subprocess import call call(["amixer", "-D", "pulse", "sset", "Master", "0%"]) Of course, you can use the normal python code with it:
valid = False while not valid: volume = input('What volume? > ') try: volume = int(volume) if (volume <= 100) and (volume >= 0): call(["amixer", "-D", "pulse", "sset", "Master", str(volume)+"%"]) valid = True except ValueError: pass This code will loop untill the user gives a valid input - between 0 and 100, and will then set the volume to that.
This will run in Python 3. Change the input to raw_input for Python 2.
To increase by 10% when the script is run you can do one of two things.
You can use the alsaaudio module.
First, install with
sudo apt-get install python-alsaaudio and then import it:
import alsaaudio we can get the volume:
>>> m = alsaaudio.Mixer() >>> vol = m.getvolume() >>> vol [50L] we can also set the volume:
>>> m.setvolume(20) >>> vol = m.getvolume() >>> vol [20L] This number is a long integer in a list. So to make it into a usable number, we can do int(vol[0]).
So to increase by 10% when it is run?
import alsaaudio m = alsaaudio.Mixer() vol = m.getvolume() vol = int(vol[0]) newVol = vol + 10 m.setvolume(newVol) Or we can stick with the subprocess module and default Ubuntu commands:
from subprocess import call call(["amixer", "-D", "pulse", "sset", "Master", "10%+"]) will increase by 10%.
My pronouns are He / Him
- thanx, it worked . Can we do this by importing alsaaudio in python and execute this based on initial volume.tarun14110– tarun141102015-10-24 22:48:28 +00:00Commented Oct 24, 2015 at 22:48
- @tarun14110 i.e. get the current volume and add an amount? SureTim– Tim2015-10-24 22:49:35 +00:00Commented Oct 24, 2015 at 22:49
- instead of asking from the user , can't I read the value of volume from my system itself ? Like whenever someone runs the script, the volume increases by 10%.tarun14110– tarun141102015-10-24 22:55:48 +00:00Commented Oct 24, 2015 at 22:55
- okk, got it thanks for helping , we can increase volume by 10% from the current volume by using this coomand "amixer -D pulse sset Master 10%+"tarun14110– tarun141102015-10-24 23:05:11 +00:00Commented Oct 24, 2015 at 23:05
- 1I have installed alsaaudio module , I have tried the thing you told about , but I am getting error "Unable to find mixer control 'Master',0 " How can I resolve that ?tarun14110– tarun141102015-10-24 23:08:36 +00:00Commented Oct 24, 2015 at 23:08
For me, Tim's code didn't quite work. I had to do this:
import alsaaudio m = alsaaudio.Mixer(alsaaudio.mixers[0]) # alsaaudio.mixers = ["PCM"] for me. m.setvolume(90) # Or whatever It may be due to my weird / broken .asoundrc config file. But given that there is no actual reference documentation for .asoundrc - just some random examples - I don't think you can blame me.
Also please don't call out to command line programs to do it. That is ugly and error-prone.
you can also use the os module:
import os os.system('amixer -D pulse sset Master '+YourVolumeVariable+'%') - Not what the question asked. It was quite clear it was to be python.David DE– David DE2025-04-15 15:21:25 +00:00Commented Apr 15 at 15:21
- @DavidDE That is Python. It's the
osmodule in Python.2025-04-15 19:28:28 +00:00Commented Apr 15 at 19:28