简体   繁体   中英

How do I change the system volume in windows using python?

How do I change the system volume in windows using python, with just using the python standard library? I know how to change the system volume by pressing the volume keys:

from ctypes import WinDLL
user32 = WinDLL("user32")

# Turns volume up:
for _ in range(50):
    user32.keybd_event(0xAF, 0, 0, 0)
    user32.keybd_event(0xAF, 0, 2, 0)

# Turns volume down:
for _ in range(50):
    user32.keybd_event(0xAE, 0, 0, 0)
    user32.keybd_event(0xAE, 0, 2, 0)

But how to I set the volume to a certain value, without using any extra python packages? I'd also like a way to get the current volume.

Edit: The suggested question does not answer my question because all the answers to that question either don't work on windows, or they use extra python packages, or they just press the volume keys, and I want way of setting the volume to a certain value and a way of getting the current volume.

This might work for you: https://docs.python.org/3/library/ossaudiodev.html

Try looking in oss_mixer_device.get and.set. I am currently on mobile but later on I can try to get you a piece of working code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM