You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Windows系统下用Python获取当前音频输出设备的音量?

在Windows系统下用Python获取音频输出设备音量的简便方法

依赖安装

首先安装pycaw库,这是专门适配Windows音频控制的Python工具库,直接通过pip安装:

pip install pycaw

代码实现

以下代码可直接获取系统默认音频输出设备的音量,返回结果支持0-1的小数或百分比形式:

from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
from comtypes import CLSCTX_ALL

# 获取默认音频输出设备
devices = AudioUtilities.GetSpeakers()
# 激活设备的音量控制接口
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
# 转为可操作的音量控制对象
volume = interface.QueryInterface(IAudioEndpointVolume)

# 获取0-1区间的小数音量值
current_volume_scalar = volume.GetMasterVolumeLevelScalar()
# 转换为0-100的百分比值
current_volume_percent = current_volume_scalar * 100

print(f"当前音量(小数):{current_volume_scalar:.2f}")
print(f"当前音量(百分比):{int(current_volume_percent)}%")

代码说明

  • AudioUtilities.GetSpeakers():直接调用系统默认的音频输出设备(扬声器)
  • GetMasterVolumeLevelScalar():Core Audio API提供的方法,直接返回标准化的0-1音量值,无需额外换算
  • 百分比形式仅需将小数结果乘以100即可,逻辑简单直观

注意事项

  • 该方案仅适用于Windows系统,依赖Windows原生的Core Audio API
  • 无需管理员权限即可正常运行,除非系统有特殊权限限制

内容的提问来源于stack exchange,提问作者Leo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 09:15:29