1

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.

1 Answers1

1

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.

  • "This module allows you to access the OSS (Open Sound System) audio interface. OSS is available for a wide range of open-source and commercial Unices, and is the standard audio interface for Linux and recent versions of FreeBSD." So probably won't help for Windows :( – Jack Deeth Feb 14 '22 at 15:01
  • 1
    Okay I think this will work: https://stackoverflow.com/questions/32149809/read-and-or-change-windows-8-master-volume-in-python – cwdesignsvs Feb 14 '22 at 18:10
  • All the answers to that question use extra python packages and I want to do it in windows with just using the python standard library. – Bevis Hobbs Feb 15 '22 at 07:41
  • May I ask why you only want to use the standard library? Sometimes you need to use extra packages to make a project work. – cwdesignsvs Feb 16 '22 at 06:22
  • The reason is because whenever I try to install an extra python package it says that there was a problem confirming the ssl certificate. I've googled it and tried various different things, but I can't get it to work. – Bevis Hobbs Feb 16 '22 at 11:23
  • The reason is because whenever I try to install an extra python package it says that there was a problem confirming the ssl certificate. I've googled it and tried various different things, but I can't get it to work. – Bevis Hobbs Feb 16 '22 at 11:29