Python Change Master/Application Volume

f.rodrigues picture f.rodrigues · Dec 29, 2013 · Viewed 22.5k times · Source

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.

Answer

jblz picture jblz · May 1, 2017

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()