通过Python调整Win10应用级别音量

然后把脚本放到Stream Deck上配置个按键, 就可以实现一键调整Chrome的音量大小了. 这样打游戏的时候可以很方便的调整Youtube的音量.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import time
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume


def get_audio_volume(process_name):
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
volume = session._ctl.QueryInterface(ISimpleAudioVolume)
print(session.Process)
if session.Process and session.Process.name().lower() == process_name:
print("{} current volume: {}".format(session.Process.name(), volume.GetMasterVolume()))
return volume.GetMasterVolume()

return None


def set_audio_volume(process_name, target_volume):
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
volume = session._ctl.QueryInterface(ISimpleAudioVolume)
print(session.Process)
if session.Process and session.Process.name().lower() == process_name:
print("{} current volume: {}".format(session.Process.name(), volume.GetMasterVolume()))
volume.SetMasterVolume(target_volume, None)
return

参考

python能否调用windows系统的音量合成器?

Trouble accessing an my DefaultAudioEndpoint.

Volume Controls - Win32 apps | Microsoft Learn