微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

阅读和/或更改Python中的Windows 8主卷

我怎样才能改变我的笔记本电脑上使用Python的主音量? 我知道一种方法,我使用ctypes来模拟音量提高/音量减小的按键,但不知道当前的音量,每当我启动应用程序进行校准时,我都必须让代码执行50个连续的降低音量的按键它为零。

有没有办法让我获得当前的系统音量,或者更好地,将主音量设置为指定的值?

我使用Python 3.4运行Windows 8 64位系统。

键盘input传递给Windows可执行文件

如何在Python中检测ESCape按键?

如何在Windows上用Python格式化硬盘?

Linux IPC:locking,但不是基于文件的locking

无法在Windows上使用pip安装任何python软件包

platform.system和platform.linux_distribution究竟输出了什么?

在Linux上安装Python程序

什么可以(安全地)从Linux上的自定义python安装删除,使其更小

MemoryLoadError当试图运行py2exe应用程序

Nginx.service:无法从文件/run/Nginx.pid中读取PID:无效的参数

Tim Roberts在关于如何在Windows上控制音量的python-win32讨论列表中发布了一个comtypes示例。 在下面的示例中,我修改了Tim的代码添加了返回认音频端点和IAudioEndpointVolume接口的类方法

我测试的两个系统(Windows 7和Windows 10)的dB音量范围将最大电平归一化为0 dB,但系统之间的最小电平和音量增量不同。 我建议使用标量和积分步骤方法。 标量范围是0.0到1.0。

import ctypes import comtypes from ctypes import wintypes MMDeviceApiLib = comtypes.GUID( '{2FDAAFA3-7523-4F66-9957-9D5E7FE698F6}') IID_IMMDevice = comtypes.GUID( '{D666063F-1587-4E43-81F1-B948E807363F}') IID_IMMDeviceCollection = comtypes.GUID( '{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}') IID_IMMDeviceEnumerator = comtypes.GUID( '{A95664D2-9614-4F35-A746-DE8DB63617E6}') IID_IAudioEndpointVolume = comtypes.GUID( '{5CDF2C82-841E-4546-9722-0CF74078229A}') CLSID_MMDeviceEnumerator = comtypes.GUID( '{BCDE0395-E52F-467C-8E3D-C4579291692E}') # EDataFlow eRender = 0 # audio rendering stream eCapture = 1 # audio capture stream eAll = 2 # audio rendering or capture stream # ERole eConsole = 0 # games,system sounds,and voice commands eMultimedia = 1 # music,movies,narration eCommunications = 2 # voice communications LPCGUID = REFIID = ctypes.POINTER(comtypes.GUID) LPFLOAT = ctypes.POINTER(ctypes.c_float) LPDWORD = ctypes.POINTER(wintypes.DWORD) LPUINT = ctypes.POINTER(wintypes.UINT) LPBOOL = ctypes.POINTER(wintypes.BOOL) PIUnkNown = ctypes.POINTER(comtypes.IUnkNown) class IMMDevice(comtypes.IUnkNown): _iid_ = IID_IMMDevice _methods_ = ( comtypes.COMMETHOD([],ctypes.HRESULT,'Activate',(['in'],REFIID,'iid'),wintypes.DWORD,'dwClsCtx'),LPDWORD,'pActivationParams',None),(['out','retval'],ctypes.POINTER(PIUnkNown),'ppInterface')),comtypes.STDMETHOD(ctypes.HRESULT,'OpenPropertyStore',[]),'GetId','GetState',[])) PIMMDevice = ctypes.POINTER(IMMDevice) class IMMDeviceCollection(comtypes.IUnkNown): _iid_ = IID_IMMDeviceCollection PIMMDeviceCollection = ctypes.POINTER(IMMDeviceCollection) class IMMDeviceEnumerator(comtypes.IUnkNown): _iid_ = IID_IMMDeviceEnumerator _methods_ = ( comtypes.COMMETHOD([],'EnumAudioEndpoints','dataFlow'),'dwStateMask'),ctypes.POINTER(PIMMDeviceCollection),'ppDevices')),comtypes.COMMETHOD([],'GetDefaultAudioEndpoint','role'),ctypes.POINTER(PIMMDevice),'ppDevices'))) @classmethod def get_default(cls,dataFlow,role): enumerator = comtypes.CoCreateInstance( CLSID_MMDeviceEnumerator,cls,comtypes.CLSCTX_INPROC_SERVER) return enumerator.GetDefaultAudioEndpoint(dataFlow,role) class IAudioEndpointVolume(comtypes.IUnkNown): _iid_ = IID_IAudioEndpointVolume _methods_ = ( comtypes.STDMETHOD(ctypes.HRESULT,'RegisterControlChangeNotify','UnregisterControlChangeNotify','GetChannelCount',LPUINT,'pnChannelCount')),'SetMasterVolumeLevel',ctypes.c_float,'fLevelDB'),LPCGUID,'pguidEventContext',None)),'SetMasterVolumeLevelScalar','fLevel'),'GetMasterVolumeLevel',LPFLOAT,'pfLevelDB')),'GetMasterVolumeLevelScalar','pfLevel')),'SetChannelVolumeLevel',wintypes.UINT,'nChannel'),'SetChannelVolumeLevelScalar','GetChannelVolumeLevel','GetChannelVolumeLevelScalar','SetMute',wintypes.BOOL,'bMute'),'GetMute',LPBOOL,'pbMute')),'GetVolumeStepInfo','pnStep'),'pnStepCount')),'VolumeStepUp','VolumeStepDown','QueryHardwareSupport','pdwHardwareSupportMask')),'GetVolumeRange','pfLevelMinDB'),'pfLevelMaxDB'),'pfVolumeIncrementDB'))) @classmethod def get_default(cls): endpoint = IMMDeviceEnumerator.get_default(eRender,eMultimedia) interface = endpoint.Activate(cls._iid_,comtypes.CLSCTX_INPROC_SERVER) return ctypes.cast(interface,ctypes.POINTER(cls))

例如:

if __name__ == '__main__': def show_vol(ev): voldb = ev.GetMasterVolumeLevel() volsc = ev.GetMasterVolumeLevelScalar() volst,nstep = ev.GetVolumeStepInfo() print('Master Volume (dB): %0.4f' % voldb) print('Master Volume (scalar): %0.4f' % volsc) print('Master Volume (step): %d / %d' % (volst,nstep)) def test(): ev = IAudioEndpointVolume.get_default() vol = ev.GetMasterVolumeLevelScalar() vmin,vmax,vinc = ev.GetVolumeRange() print('Volume Range (min,max,step) (dB): ' '%0.4f,%0.4f,%0.4f' % (vmin,vinc)) show_vol(ev) try: print('nIncrement the master volume') ev.VolumeStepUp() show_vol(ev) print('nDecrement the master volume twice') ev.VolumeStepDown() ev.VolumeStepDown() show_vol(ev) print('nSet the master volume to 0.75 scalar') ev.SetMasterVolumeLevelScalar(0.75) show_vol(ev) print('nSet the master volume to 0.25 scalar') ev.SetMasterVolumeLevelScalar(0.25) show_vol(ev) finally: ev.SetMasterVolumeLevelScalar(vol) comtypes.CoInitialize() try: test() finally: comtypes.CoUninitialize()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐