Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Change Master/Application Volume

How do I change it?

I'm looking for something like:

SetMasterVolume(0.5)
SetAppVolume('FooBar',0.5)

I tried using ctypes.windll.winmm, but I can't find much documentation on how to use it.

Thanks in advance.

like image 325
f.rodrigues Avatar asked Dec 29 '13 18:12

f.rodrigues


People also ask

How do you mute in Python?

Every call to Sound. mute() will toggle mute on or off.


2 Answers

I'd hope after 5 years this is no longer a problem for you, but I've just had to do the same thing. It's possible using the PyCaw library.

Simple proof of concept, based on PyCaw's examples

from __future__ import print_function
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume


def main():
    sessions = AudioUtilities.GetAllSessions()
    for session in sessions:
        volume = session._ctl.QueryInterface(ISimpleAudioVolume)
        if session.Process and session.Process.name() == "vlc.exe":
            print("volume.GetMasterVolume(): %s" % volume.GetMasterVolume())
            volume.SetMasterVolume(0.6, None)


if __name__ == "__main__":
    main()
like image 71
jblz Avatar answered Sep 24 '22 11:09

jblz


this is a very roundabout way to do it but it works.you can simulate the key presses for the computers master volume with pynput. it works but it is quite inacurate.

from pynput.keyboard import Key,Controller
keyboard = Controller()
import time
while True:
    for i in range(10):
        keyboard.press(Key.media_volume_up)
        keyboard.release(Key.media_volume_up)
        time.sleep(0.1)
    for i in range(10):
        keyboard.press(Key.media_volume_down)
        keyboard.release(Key.media_volume_down)
        time.sleep(0.1)
    time.sleep(2) 

this is an ok method of doing it. every keypress and release is equal to about 2 volume . hope this is somewhat helpful!

like image 21
dragon445 Avatar answered Sep 20 '22 11:09

dragon445