如何使用Python/pycaw简单获取PC的主音量。

huangapple go评论68阅读模式
英文:

How to simply get the master volume of a PC with python/pycaw

问题

以下是翻译好的内容:

一个绅士在9个月前提出了这个问题。它从未得到正确回答,而且帖子已关闭。他被告知要执行以下操作:

在Windows上,使用pycaw:

from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume

devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))

volume.GetMasterVolumeLevelScalar()

帖子随后被关闭,但这并不是答案,这个标量级别并不是在PC监视器系统托盘中显示的主音量的0-100级别。我已经安装了pycaw和comtypes,并且使用了当前的Python 3.11.1版本,已经在我的Python中输入了上述所有命令。请问我应该使用什么命令来获取我的主音量的确切输出。如果我的PC音量是50,我希望得到一个输出为50。使用上述说明,在PC音量为100级时,标量级别输出为1。对我来说毫无价值。请帮忙。

我尝试使用以下命令:

volume.GetMasterVolume()

希望得到主音量的返回值。但我得到了一个错误:

>>> volume.GetMasterVolume()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'POINTER(IAudioEndpointVolume)' object has no attribute 'GetMasterVolume'. Did you mean: 'GetMasterVolumeLevel'?

所以我尝试了volume.GetMasterVolumeLevel(),在100主音量时返回了0.0,同样毫无价值。

为什么volume.GetMasterVolume()不起作用?我需要做什么才能让它工作?

英文:

A gentleman asked this question 9 months ago. It was never answered correctly and the thread was closed. He was told to do this:

On Windows, use pycaw:

from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))

volume.GetMasterVolumeLevelScalar()

And with this, the thread was closed but this is NOT the answer, this scalar level is NOT the 0-100level of master volume displayed on a PC monitor in the systray. I have pycaw and comtypes installed and am on the current python 3.11.1. I have entered all the above commands into my python. Please may I know what command to use to get the exact output of my master volume. If my pc volume is 50 I want an output of 50. With the above instructions, at pc volume level 100, the scalar level output is 1. Thats worthless to me. Please help.

I tried using the command

volume.GetMasterVolume()

hoping to get the master level return. instead I got an error:

&gt;&gt;&gt; volume.GetMasterVolume()
Traceback (most recent call last):
File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
AttributeError: &#39;POINTER(IAudioEndpointVolume)&#39; object has no attribute &#39;GetMasterVolume&#39;. Did      you mean: &#39;GetMasterVolumeLevel&#39;?

So I tried volume.GetMasterVolumeLevel() and at 100 master volume this returned 0.0 again worthless.

Why does volume.GetMasterVolume() not work? What do I have to do to make it work?

答案1

得分: 1

在我自己运行代码之后,我发现你提供的代码确实按照你的要求正常工作。然而,不同之处在于volume是在0到1之间的数字,而不是在0到100之间。

你只需要进行一个简单的更改:

from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))

vol = volume.GetMasterVolumeLevelScalar() * 100
vol_str = f&quot;{vol:.0f}%&quot;
print(vol_str)

这会将音量打印为整数百分比。如果你想要不同的数字/显示格式,请根据需要修改vol_str

另请参考:

英文:

After running the code myself, I see that the code you have provided does actually work as you want. The difference, however, is that volume is a number between 0 and 1, instead of 0 and 100.

All you need is a simple change:

from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))

vol = volume.GetMasterVolumeLevelScalar() * 100
vol_str = f&quot;{vol:.0f}%&quot;
print(vol_str)

This prints the volume as an integer percentage. If you want a different number/display format, modify vol_str as needed.

See also:

huangapple
  • 本文由 发表于 2023年5月7日 13:55:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定