精确控制windows全局音量(Python)

摘要:
不用多说,代码是:1importctypes,time2importcomtypes3fromtypesImportwintypes45MMDeviceApiLib=comtypes。GUID(6'{2FDAFA3-7523-4F66-9957-9D5E7FE698F6}')7IID_IMMDevice=comtypes。GUID(8'{D666063F-1587-4E43-81F1-B9

话不多说,直接上代码:

  1 import ctypes,time
  2 import comtypes
  3 from ctypes import wintypes
  4 
  5 MMDeviceApiLib = comtypes.GUID(
  6     '{2FDAAFA3-7523-4F66-9957-9D5E7FE698F6}')
  7 IID_IMMDevice = comtypes.GUID(
  8     '{D666063F-1587-4E43-81F1-B948E807363F}')
  9 IID_IMMDeviceCollection = comtypes.GUID(
 10     '{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}')
 11 IID_IMMDeviceEnumerator = comtypes.GUID(
 12     '{A95664D2-9614-4F35-A746-DE8DB63617E6}')
 13 IID_IAudioEndpointVolume = comtypes.GUID(
 14     '{5CDF2C82-841E-4546-9722-0CF74078229A}')
 15 CLSID_MMDeviceEnumerator = comtypes.GUID(
 16     '{BCDE0395-E52F-467C-8E3D-C4579291692E}')
 17 
 18 # EDataFlow
 19 eRender = 0 # audio rendering stream
 20 eCapture = 1 # audio capture stream
 21 eAll = 2 # audio rendering or capture stream
 22 
 23 # ERole
 24 eConsole = 0 # games, system sounds, and voice commands
 25 eMultimedia = 1 # music, movies, narration
 26 eCommunications = 2 # voice communications
 27 
 28 LPCGUID = REFIID = ctypes.POINTER(comtypes.GUID)
 29 LPFLOAT = ctypes.POINTER(ctypes.c_float)
 30 LPDWORD = ctypes.POINTER(wintypes.DWORD)
 31 LPUINT = ctypes.POINTER(wintypes.UINT)
 32 LPBOOL = ctypes.POINTER(wintypes.BOOL)
 33 PIUnknown = ctypes.POINTER(comtypes.IUnknown)
 34 
 35 class IMMDevice(comtypes.IUnknown):
 36     _iid_ = IID_IMMDevice
 37     _methods_ = (
 38         comtypes.COMMETHOD([], ctypes.HRESULT, 'Activate',
 39             (['in'], REFIID, 'iid'),
 40             (['in'], wintypes.DWORD, 'dwClsCtx'),
 41             (['in'], LPDWORD, 'pActivationParams', None),
 42             (['out','retval'], ctypes.POINTER(PIUnknown), 'ppInterface')),
 43         comtypes.STDMETHOD(ctypes.HRESULT, 'OpenPropertyStore', []),
 44         comtypes.STDMETHOD(ctypes.HRESULT, 'GetId', []),
 45         comtypes.STDMETHOD(ctypes.HRESULT, 'GetState', []))
 46 
 47 PIMMDevice = ctypes.POINTER(IMMDevice)
 48 
 49 class IMMDeviceCollection(comtypes.IUnknown):
 50     _iid_ = IID_IMMDeviceCollection
 51 
 52 PIMMDeviceCollection = ctypes.POINTER(IMMDeviceCollection)
 53 
 54 class IMMDeviceEnumerator(comtypes.IUnknown):
 55     _iid_ = IID_IMMDeviceEnumerator
 56     _methods_ = (
 57         comtypes.COMMETHOD([], ctypes.HRESULT, 'EnumAudioEndpoints',
 58             (['in'], wintypes.DWORD, 'dataFlow'),
 59             (['in'], wintypes.DWORD, 'dwStateMask'),
 60             (['out','retval'], ctypes.POINTER(PIMMDeviceCollection),
 61              'ppDevices')),
 62         comtypes.COMMETHOD([], ctypes.HRESULT, 'GetDefaultAudioEndpoint',
 63             (['in'], wintypes.DWORD, 'dataFlow'),
 64             (['in'], wintypes.DWORD, 'role'),
 65             (['out','retval'], ctypes.POINTER(PIMMDevice), 'ppDevices')))
 66     @classmethod
 67     def get_default(cls, dataFlow, role):
 68         enumerator = comtypes.CoCreateInstance(
 69             CLSID_MMDeviceEnumerator, cls, comtypes.CLSCTX_INPROC_SERVER)
 70         return enumerator.GetDefaultAudioEndpoint(dataFlow, role)
 71 
 72 class IAudioEndpointVolume(comtypes.IUnknown):
 73     _iid_ = IID_IAudioEndpointVolume
 74     _methods_ = (
 75         comtypes.STDMETHOD(ctypes.HRESULT, 'RegisterControlChangeNotify', []),
 76         comtypes.STDMETHOD(ctypes.HRESULT, 'UnregisterControlChangeNotify', []),
 77         comtypes.COMMETHOD([], ctypes.HRESULT, 'GetChannelCount',
 78             (['out', 'retval'], LPUINT, 'pnChannelCount')),
 79         comtypes.COMMETHOD([], ctypes.HRESULT, 'SetMasterVolumeLevel',
 80             (['in'], ctypes.c_float, 'fLevelDB'),
 81             (['in'], LPCGUID, 'pguidEventContext', None)),
 82         comtypes.COMMETHOD([], ctypes.HRESULT, 'SetMasterVolumeLevelScalar',
 83             (['in'], ctypes.c_float, 'fLevel'),
 84             (['in'], LPCGUID, 'pguidEventContext', None)),
 85         comtypes.COMMETHOD([], ctypes.HRESULT, 'GetMasterVolumeLevel',
 86             (['out','retval'], LPFLOAT, 'pfLevelDB')),
 87         comtypes.COMMETHOD([], ctypes.HRESULT, 'GetMasterVolumeLevelScalar',
 88             (['out','retval'], LPFLOAT, 'pfLevel')),
 89         comtypes.COMMETHOD([], ctypes.HRESULT, 'SetChannelVolumeLevel',
 90             (['in'], wintypes.UINT, 'nChannel'),
 91             (['in'], ctypes.c_float, 'fLevelDB'),
 92             (['in'], LPCGUID, 'pguidEventContext', None)),
 93         comtypes.COMMETHOD([], ctypes.HRESULT, 'SetChannelVolumeLevelScalar',
 94             (['in'], wintypes.UINT, 'nChannel'),
 95             (['in'], ctypes.c_float, 'fLevel'),
 96             (['in'], LPCGUID, 'pguidEventContext', None)),
 97         comtypes.COMMETHOD([], ctypes.HRESULT, 'GetChannelVolumeLevel',
 98             (['in'], wintypes.UINT, 'nChannel'),
 99             (['out','retval'], LPFLOAT, 'pfLevelDB')),
100         comtypes.COMMETHOD([], ctypes.HRESULT, 'GetChannelVolumeLevelScalar',
101             (['in'], wintypes.UINT, 'nChannel'),
102             (['out','retval'], LPFLOAT, 'pfLevel')),
103         comtypes.COMMETHOD([], ctypes.HRESULT, 'SetMute',
104             (['in'], wintypes.BOOL, 'bMute'),
105             (['in'], LPCGUID, 'pguidEventContext', None)),
106         comtypes.COMMETHOD([], ctypes.HRESULT, 'GetMute',
107             (['out','retval'], LPBOOL, 'pbMute')),
108         comtypes.COMMETHOD([], ctypes.HRESULT, 'GetVolumeStepInfo',
109             (['out','retval'], LPUINT, 'pnStep'),
110             (['out','retval'], LPUINT, 'pnStepCount')),
111         comtypes.COMMETHOD([], ctypes.HRESULT, 'VolumeStepUp',
112             (['in'], LPCGUID, 'pguidEventContext', None)),
113         comtypes.COMMETHOD([], ctypes.HRESULT, 'VolumeStepDown',
114             (['in'], LPCGUID, 'pguidEventContext', None)),
115         comtypes.COMMETHOD([], ctypes.HRESULT, 'QueryHardwareSupport',
116             (['out','retval'], LPDWORD, 'pdwHardwareSupportMask')),
117         comtypes.COMMETHOD([], ctypes.HRESULT, 'GetVolumeRange',
118             (['out','retval'], LPFLOAT, 'pfLevelMinDB'),
119             (['out','retval'], LPFLOAT, 'pfLevelMaxDB'),
120             (['out','retval'], LPFLOAT, 'pfVolumeIncrementDB')))
121     @classmethod
122     def get_default(cls):
123         endpoint = IMMDeviceEnumerator.get_default(eRender, eMultimedia)
124         interface = endpoint.Activate(cls._iid_, comtypes.CLSCTX_INPROC_SERVER)
125         return ctypes.cast(interface, ctypes.POINTER(cls))
126 
127 
128 
129 
130 
131 if __name__ == '__main__':
132 
133     def show_vol(ev):
134         time.sleep(2)
135         voldb = ev.GetMasterVolumeLevel()
136         volsc = ev.GetMasterVolumeLevelScalar()
137         volst, nstep = ev.GetVolumeStepInfo()
138         print('Master Volume (dB): %0.4f' % voldb)
139         print('Master Volume (scalar): %0.4f' % volsc)#当前音量
140         print('Master Volume (step): %d / %d' % (volst, nstep))
141 
142     def test():
143         ev = IAudioEndpointVolume.get_default()
144         vol = ev.GetMasterVolumeLevelScalar()
145         vmin, vmax, vinc = ev.GetVolumeRange()
146         print('Volume Range (min, max, step) (dB): '
147               '%0.4f, %0.4f, %0.4f' % (vmin, vmax, vinc))
148         show_vol(ev)
149         try:
150             print('
Increment the master volume')
151             ev.VolumeStepUp()#音量加
152             show_vol(ev)
153             print('
Decrement the master volume twice')
154             ev.VolumeStepDown()#音量减
155             ev.VolumeStepDown()
156             show_vol(ev)
157             print('
Set the master volume to 0.75 scalar')
158             ev.SetMasterVolumeLevelScalar(0.75)#音量百分比设置
159             show_vol(ev)
160             print('
Set the master volume to 0.25 scalar')
161             ev.SetMasterVolumeLevelScalar(0.25)#音量百分比设置
162             show_vol(ev)
163         finally:
164             ev.SetMasterVolumeLevelScalar(vol)
165 
166     comtypes.CoInitialize()
167     try:
168         test()
169     finally:
170         comtypes.CoUninitialize()

简单逻辑就是先获取ev对象(

ev = IAudioEndpointVolume.get_default()

然后有几个方法:

方法一(获取当前音量):(

volsc = ev.GetMasterVolumeLevelScalar()

方法二(音量加/减):(

ev.VolumeStepUp()#音量加
ev.VolumeStepDown()#音量减

方法三(设置音量):(

ev.SetMasterVolumeLevelScalar(0.75)#音量百分比设置

另附简单调用win32API加减音量:

import ctypes,operator

WM_APPCOMMAND = 0x319
APPCOMMAND_VOLUME_UP = 0x0a
APPCOMMAND_VOLUME_DOWN = 0x09
APPCOMMAND_VOLUME_MUTE = 0x08

def setSpeakerVol(volume):
        hwnd = ctypes.windll.user32.GetForegroundWindow()

        # ctypes.windll.winmm.waveOutSetVolume(hwnd, 0xffffffff)

        print(hwnd)
        ctypes.windll.user32.PostMessageA(hwnd, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_DOWN * 0x10000)
#
if __name__ == '__main__':

    setSpeakerVol(0)

利用:

PostMessageA

免责声明:文章转载自《精确控制windows全局音量(Python)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇spark 2.X 疑难问题汇总Bartender 打印RFID标签 (使用C#_SDK进行RFID打印或者表面信息打印)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python系列:二、数据类型--技术流ken

标准数据类型 Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Set(集合) Dictionary(字典) Python3 的六个标准数据类型中: 不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组); 可变数据(...

python 类型注解

函数定义的弊端 python 是动态语言,变量随时可以被赋值,且能赋值为不同类型 python 不是静态编译型语言,变量类型是在运行器决定的 动态语言很灵活,但是这种特性也是弊端 def add(x,y): return x+y print(add(4,5)) print(add('hello','world')) print(add(...

文本检测和识别 代码结构梳理

前言:最近学习了一些OCR相关的基础知识,包含目标检测和自然语言处理。 正好,在数字中国有相关的比赛: https://www.datafountain.cn/competitions/334/details/rule 所以想动手实践一下,实际中发现,对于数据标签的处理和整个检测和识别的流程并不熟悉,自己从头去搞还是有很大难度。 幸好,有大佬们之前开源的一...

python程序打包成.exe----pyinstaller工具

1. 环境 windows 2. 安装 准备文件:PyWin32 or pypiwin32  运行如下安装命令:  pip install pyinstaller==3.0 不要使用3.2版本,编译完成后会报Runtime Error, R6034错误. 3.0版本无此问题。 3. 打包 把.py或.pyw文件拷贝到pyinstaller所在目录 执行命令...

python之selenium操作隐藏元素

    Fast traslate Icon translate     getElementsByTagName   GetElementsByTagName Fast traslate Icon translate 一、html显示与隐藏元素的几种方法 1、display none:...

guxh的python笔记三:装饰器

1,函数作用域 这种情况可以顺利执行: total = 0 def run(): print(total) 这种情况会报错: total = 0 def run(): print(total) total = 1 这种情况也会报错: total = 0 def run(): total += 1...