简体   繁体   English

如何使用 python 更改 windows 中的系统音量?

[英]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?如何使用 python 更改 windows 中的系统音量,使用 python 标准库? 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?但是如何在不使用任何额外的 python 包的情况下将音量设置为某个值? 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.编辑:建议的问题没有回答我的问题,因为该问题的所有答案都不适用于 windows,或者他们使用额外的 python 包,或者他们只是按下音量键,我想要将音量设置为一定的价值和获得当前音量的方法。

This might work for you: https://docs.python.org/3/library/ossaudiodev.html这可能对您有用:https://docs.python.org/3/library/ossaudiodev.html

Try looking in oss_mixer_device.get and.set.尝试查看 oss_mixer_device.get 和 .set。 I am currently on mobile but later on I can try to get you a piece of working code.我目前在移动设备上,但稍后我可以尝试为您提供一段工作代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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