Python Change Master/Application Volume Python Change Master/Application Volume windows windows

Python Change Master/Application Volume


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_functionfrom pycaw.pycaw import AudioUtilities, ISimpleAudioVolumedef 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()


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,Controllerkeyboard = Controller()import timewhile 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!


I know this is too late but if someone is still looking for a seamless solution

Download this exe application from here

and just call this command using python

./SoundVolumeView.exe /SetVolume "High Definition Audio Device\Device\Speakers" 50