如何使用 pywin32 mute/unmute 发声?

How to mute/unmute sound using pywin32?

我的搜索引导我找到 Pywin32,它应该能够 mute/unmute 声音并检测其状态(在 Windows 10 上,使用 Python 3+)。我找到了一种使用 AutoHotkey 脚本的方法,但我正在寻找一种 pythonic 方法。

更具体地说,我对使用 Windows GUI 不感兴趣。 Pywin32 使用 Windows DLL.

到目前为止,我可以通过调用 ahk 脚本来完成:

在 python 脚本中:

import subprocess
subprocess.call([ahkexe, ahkscript])

在 AutoHotkey 脚本中:

SoundGet, sound_mute, Master, mute
if sound_mute = On ; if the sound is muted
Send {Volume_Mute} ; press the "mute button" to unmute
SoundSet 30 ; set the sound level at 30

您可以使用悖论 (https://github.com/Paradoxis/Windows-Sound-Manager) 的 Windows 声音管理器。

from sound import Sound
Sound.mute()

每次调用 Sound.mute() 都会打开或关闭静音。查看 main.py 了解如何使用 setter 和 getter 方法。