gpt4 book ai didi

python - 在 Windows 7 上以编程方式更改系统范围内的扬声器平衡

转载 作者:太空狗 更新时间:2023-10-29 21:05:43 24 4
gpt4 key购买 nike

如何在 Windows 7 上以编程方式更改系统范围内的扬声器平衡?

(我只会用 Python 和 AHK 编程。)

最佳答案

快捷方式(由@AndreMiras 创建)--> 使用 Pycaw ,Pycaw 的用法可以找到here

详细方法 --> 首先安装 python 包 Comtypes 要么通过

  1. 使用 pip 安装 comtypes

  2. https://pypi.python.org/pypi/comtypes下载然后解压下载文件夹。在提取的文件夹中找到 setup.py。打开cmd进入setup.py路径,然后运行命令python setup.py install

现在只需运行 code.py 文件,您可以通过更改函数 SetMasterVolumeLevel(-25, None) 的第一个参数来调整音量,第一个参数必须是在 -65 到 0 之间。我已经在 Windows 8 中尝试过这段代码,希望它能在 Windows 7 上运行。

查看代码的最后几行以找到 volume.SetMasterVolumeLevel(-25, None) 并调整 PC 的音量

代码.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

关于python - 在 Windows 7 上以编程方式更改系统范围内的扬声器平衡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25631123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com