Python 检查系统卷是否已静音?

Python 检查系统卷是否已静音?,python,windows,volume,mute,Python,Windows,Volume,Mute,我正在做一个项目,为部分节目播放音频。如果用户的系统卷被静音,我希望能够显示一条消息。我在Windows上使用Python。使用Windows Mixer API。我为您找到了这篇文章,并附上了相关代码: (摘自MS KB 181550:“监控音频音量级别”) 这是用C编写的,但也可以“翻译”成Python 我希望这有帮助 #include <windows.h> #include <mmsystem.h> MMRESULT rc; // Re

我正在做一个项目,为部分节目播放音频。如果用户的系统卷被静音,我希望能够显示一条消息。我在Windows上使用Python。

使用Windows Mixer API。我为您找到了这篇文章,并附上了相关代码: (摘自MS KB 181550:“监控音频音量级别”)

这是用C编写的,但也可以“翻译”成Python

我希望这有帮助

#include <windows.h>
#include <mmsystem.h>

MMRESULT rc;              // Return code.
HMIXER hMixer;            // Mixer handle used in mixer API calls.
MIXERCONTROL mxc;         // Holds the mixer control data.
MIXERLINE mxl;            // Holds the mixer line data.
MIXERLINECONTROLS mxlc;   // Obtains the mixer control.

// Open the mixer. This opens the mixer with a deviceID of 0. If you
// have a single sound card/mixer, then this will open it. If you have
// multiple sound cards/mixers, the deviceIDs will be 0, 1, 2, and
// so on.
rc = mixerOpen(&hMixer, 0,0,0,0);
if (MMSYSERR_NOERROR != rc) {
    // Couldn't open the mixer.
}

// Initialize MIXERLINE structure.
ZeroMemory(&mxl,sizeof(mxl));
mxl.cbStruct = sizeof(mxl);

// Specify the line you want to get. You are getting the input line
// here. If you want to get the output line, you need to use
// MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT.
mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;

rc = mixerGetLineInfo((HMIXEROBJ)hMixer, &mxl,
                       MIXER_GETLINEINFOF_COMPONENTTYPE);
if (MMSYSERR_NOERROR == rc) {
    // Couldn't get the mixer line.
}

// Get the control.
ZeroMemory(&mxlc, sizeof(mxlc));
mxlc.cbStruct = sizeof(mxlc);
mxlc.dwLineID = mxl.dwLineID;
mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_PEAKMETER;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeof(mxc);
mxlc.pamxctrl = &mxc;
ZeroMemory(&mxc, sizeof(mxc));
mxc.cbStruct = sizeof(mxc);
rc = mixerGetLineControls((HMIXEROBJ)hMixer,&mxlc,
                           MIXER_GETLINECONTROLSF_ONEBYTYPE);
if (MMSYSERR_NOERROR != rc) {
    // Couldn't get the control.
}

// After successfully getting the peakmeter control, the volume range
// will be specified by mxc.Bounds.lMinimum to mxc.Bounds.lMaximum.

MIXERCONTROLDETAILS mxcd;             // Gets the control values.
MIXERCONTROLDETAILS_SIGNED volStruct; // Gets the control values.
long volume;                          // Holds the final volume value.

// Initialize the MIXERCONTROLDETAILS structure
ZeroMemory(&mxcd, sizeof(mxcd));
mxcd.cbStruct = sizeof(mxcd);
mxcd.cbDetails = sizeof(volStruct);
mxcd.dwControlID = mxc.dwControlID;
mxcd.paDetails = &volStruct;
mxcd.cChannels = 1;

// Get the current value of the peakmeter control. Typically, you
// would set a timer in your program to query the volume every 10th
// of a second or so.
rc = mixerGetControlDetails((HMIXEROBJ)hMixer, &mxcd,
                             MIXER_GETCONTROLDETAILSF_VALUE);
if (MMSYSERR_NOERROR == rc) {
    // Couldn't get the current volume.
}
volume = volStruct.lValue;

// Get the absolute value of the volume.
if (volume < 0)
    volume = -volume;
#包括
#包括
MMRESULT rc;//返回码。
HMIXER HMIXER;//混合器API调用中使用的混合器句柄。
混音器控制mxc;//保存混合器控制数据。
混音器线mxl;//保存混音器行数据。
MIXERLINECONTROLS mxlc;//获取混音器控件。
//打开搅拌机。这将打开deviceID为0的混合器。如果你
//有一个声卡/混音器,然后这将打开它。如果你有
//多个声卡/混音器,设备ID将为0、1、2和
//等等。
rc=混合器打开(&hMixer,0,0,0);
如果(MMSYSERR\u NOERROR!=rc){
//无法打开搅拌机。
}
//初始化MIXERLINE结构。
零内存(&mxl,sizeof(mxl));
mxl.cbStruct=sizeof(mxl);
//指定要获取的行。您正在获取输入行
//在这里。如果要获得输出行,需要使用
//混音器线路\组件类型\ SRC \波形输出。
mxl.dwComponentType=混频器线_COMPONENTTYPE_DST_WAVEIN;
rc=混频器线性信息((HMIXEROBJ)hMixer和mxl,
混合器(GETLINEINFOF\u组件类型);
如果(MMSYSERR_NOERROR==rc){
//无法获取混音器线路。
}
//获得控制权。
零内存(&mxlc,sizeof(mxlc));
mxlc.cbStruct=sizeof(mxlc);
mxlc.dwLineID=mxl.dwLineID;
mxlc.dwControlType=混频器控制\控制类型\峰值计;
mxlc.cControl=1;
mxlc.cbmxctrl=sizeof(mxc);
mxlc.pamxctrl=&mxc;
零内存(&mxc,sizeof(mxc));
mxc.cbStruct=sizeof(mxc);
rc=混频器线性控制((HMIXEROBJ)hMixer和mxlc,
混合器\u GETLINECONTROLSF\u按类型);
如果(MMSYSERR\u NOERROR!=rc){
//无法控制。
}
//成功获得峰值计控制后,音量范围
//将由mxc.Bounds.lmminimum指定为mxc.Bounds.lMaximum。
MIXERCONTROLDETAILS mxcd;//获取控件值。
MIXERCONTROLDETAILS\u签名的volStruct;//获取控件值。
长卷;//保存最终体积值。
//初始化MIXERCONTROLDETAILS结构
零内存(&mxcd,sizeof(mxcd));
mxcd.cbStruct=sizeof(mxcd);
mxcd.cbDetails=sizeof(volStruct);
mxcd.dwControlID=mxc.dwControlID;
mxcd.paDetails=&volStruct;
mxcd.c通道=1;
//获取peakmeter控件的当前值。通常,你
//将在程序中设置一个计时器,每10分钟查询一次卷
//一秒钟左右。
rc=混频器控制详细信息((HMIXEROBJ)hMixer和mxcd,
混合器\u GETCONTROLDETAILSF\u值);
如果(MMSYSERR_NOERROR==rc){
//无法获取当前卷。
}
体积=volStruct.lValue;
//获取体积的绝对值。
如果(体积<0)
体积=-体积;