在Windows编程改变整个系统的扬声器平衡7 [英] Programmatically changing system-wide speaker balance on Windows 7
问题描述
我如何编程方式更改在Windows 7系统范围内的扬声器平衡?
How can I programmatically change system-wide speaker balance on Windows 7?
(我只能在Python和AHK程序。)
(I can only program in Python and AHK.)
推荐答案
首先安装Python包Comtypes或者通过
First install python package Comtypes either by
-
使用的 PIP安装comtypes 的
或
https://pypi.python.org/pypi/comtypes 下载然后解压下载文件夹。找到解压的文件夹中setup.py。打开CMD去的 setup.py 的路径,然后运行该命令的蟒蛇的setup.py安装的
download from https://pypi.python.org/pypi/comtypes and then extract the download folder. Find setup.py in extracted folder . Open cmd go to the path of setup.py and then run the command python setup.py install
现在只需运行code.py文件,你可以通过改变功能的 SetMasterVolumeLevel第一个参数( -25 的,无)第一调节音量参数必须在0〜-65到。
我曾尝试在Windows 8这个code希望它会在Windows 7上运行。
Now just run the code.py file and you can adjust the volume by changing the first argument of function SetMasterVolumeLevel(-25, None) the first argument must be in between -65 to 0. I have tried this code in windows 8 hopefully it will work on windows 7.
参见code的最后一行找到的 volume.SetMasterVolumeLevel(-25,无)并以adust PC的体积
See at the last lines of code to find the volume.SetMasterVolumeLevel(-25, None) and to adust the volume of PC
code.py
from comtypes import *
import comtypes.client
from ctypes import POINTER
from ctypes.wintypes import DWORD, BOOL
MMDeviceApiLib = \
GUID('{2FDAAFA3-7523-4F66-9957-9D5E7FE698F6}')
IID_IMMDevice = \
GUID('{D666063F-1587-4E43-81F1-B948E807363F}')
IID_IMMDeviceEnumerator = \
GUID('{A95664D2-9614-4F35-A746-DE8DB63617E6}')
CLSID_MMDeviceEnumerator = \
GUID('{BCDE0395-E52F-467C-8E3D-C4579291692E}')
IID_IMMDeviceCollection = \
GUID('{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}')
IID_IAudioEndpointVolume = \
GUID('{5CDF2C82-841E-4546-9722-0CF74078229A}')
class IMMDeviceCollection(IUnknown):
_iid_ = GUID('{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}')
pass
class IAudioEndpointVolume(IUnknown):
_iid_ = GUID('{5CDF2C82-841E-4546-9722-0CF74078229A}')
_methods_ = [
STDMETHOD(HRESULT, 'RegisterControlChangeNotify', []),
STDMETHOD(HRESULT, 'UnregisterControlChangeNotify', []),
STDMETHOD(HRESULT, 'GetChannelCount', []),
COMMETHOD([], HRESULT, 'SetMasterVolumeLevel',
(['in'], c_float, 'fLevelDB'),
(['in'], POINTER(GUID), 'pguidEventContext')
),
COMMETHOD([], HRESULT, 'SetMasterVolumeLevelScalar',
(['in'], c_float, 'fLevelDB'),
(['in'], POINTER(GUID), 'pguidEventContext')
),
COMMETHOD([], HRESULT, 'GetMasterVolumeLevel',
(['out','retval'], POINTER(c_float), 'pfLevelDB')
),
COMMETHOD([], HRESULT, 'GetMasterVolumeLevelScalar',
(['out','retval'], POINTER(c_float), 'pfLevelDB')
),
COMMETHOD([], HRESULT, 'SetChannelVolumeLevel',
(['in'], DWORD, 'nChannel'),
(['in'], c_float, 'fLevelDB'),
(['in'], POINTER(GUID), 'pguidEventContext')
),
COMMETHOD([], HRESULT, 'SetChannelVolumeLevelScalar',
(['in'], DWORD, 'nChannel'),
(['in'], c_float, 'fLevelDB'),
(['in'], POINTER(GUID), 'pguidEventContext')
),
COMMETHOD([], HRESULT, 'GetChannelVolumeLevel',
(['in'], DWORD, 'nChannel'),
(['out','retval'], POINTER(c_float), 'pfLevelDB')
),
COMMETHOD([], HRESULT, 'GetChannelVolumeLevelScalar',
(['in'], DWORD, 'nChannel'),
(['out','retval'], POINTER(c_float), 'pfLevelDB')
),
COMMETHOD([], HRESULT, 'SetMute',
(['in'], BOOL, 'bMute'),
(['in'], POINTER(GUID), 'pguidEventContext')
),
COMMETHOD([], HRESULT, 'GetMute',
(['out','retval'], POINTER(BOOL), 'pbMute')
),
COMMETHOD([], HRESULT, 'GetVolumeStepInfo',
(['out','retval'], POINTER(c_float), 'pnStep'),
(['out','retval'], POINTER(c_float), 'pnStepCount'),
),
COMMETHOD([], HRESULT, 'VolumeStepUp',
(['in'], POINTER(GUID), 'pguidEventContext')
),
COMMETHOD([], HRESULT, 'VolumeStepDown',
(['in'], POINTER(GUID), 'pguidEventContext')
),
COMMETHOD([], HRESULT, 'QueryHardwareSupport',
(['out','retval'], POINTER(DWORD), 'pdwHardwareSupportMask')
),
COMMETHOD([], HRESULT, 'GetVolumeRange',
(['out','retval'], POINTER(c_float), 'pfMin'),
(['out','retval'], POINTER(c_float), 'pfMax'),
(['out','retval'], POINTER(c_float), 'pfIncr')
),
]
class IMMDevice(IUnknown):
_iid_ = GUID('{D666063F-1587-4E43-81F1-B948E807363F}')
_methods_ = [
COMMETHOD([], HRESULT, 'Activate',
(['in'], POINTER(GUID), 'iid'),
(['in'], DWORD, 'dwClsCtx'),
(['in'], POINTER(DWORD), 'pActivationParans'),
(['out','retval'], POINTER(POINTER(IAudioEndpointVolume)), 'ppInterface')
),
STDMETHOD(HRESULT, 'OpenPropertyStore', []),
STDMETHOD(HRESULT, 'GetId', []),
STDMETHOD(HRESULT, 'GetState', [])
]
pass
class IMMDeviceEnumerator(comtypes.IUnknown):
_iid_ = GUID('{A95664D2-9614-4F35-A746-DE8DB63617E6}')
_methods_ = [
COMMETHOD([], HRESULT, 'EnumAudioEndpoints',
(['in'], DWORD, 'dataFlow'),
(['in'], DWORD, 'dwStateMask'),
(['out','retval'], POINTER(POINTER(IMMDeviceCollection)), 'ppDevices')
),
COMMETHOD([], HRESULT, 'GetDefaultAudioEndpoint',
(['in'], DWORD, 'dataFlow'),
(['in'], DWORD, 'role'),
(['out','retval'], POINTER(POINTER(IMMDevice)), 'ppDevices')
)
]
enumerator = comtypes.CoCreateInstance(
CLSID_MMDeviceEnumerator,
IMMDeviceEnumerator,
comtypes.CLSCTX_INPROC_SERVER
)
print enumerator
endpoint = enumerator.GetDefaultAudioEndpoint( 0, 1 )
print endpoint
volume = endpoint.Activate( IID_IAudioEndpointVolume, comtypes.CLSCTX_INPROC_SERVER, None )
print volume
print volume.GetMasterVolumeLevel()
print volume.GetVolumeRange()
#volume.SetMasterVolumeLevel(-65, None) uncomment for 0 volume
#volume.SetMasterVolumeLevel(-1, None) uncomment for full volume
volume.SetMasterVolumeLevel(-25, None) #Change the first argument for controlling the volume remember it should be -ve not less than -65
这篇关于在Windows编程改变整个系统的扬声器平衡7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!