在Windows XP / Vista / Seven中获取主音量(通过键盘轮正常增加)

时间:2011-01-07 20:12:15

标签: c# windows audio

我知道很多人都问过这个,但我没有看到任何答案,是否有任何lib /通用的GET方式(未设置,我不需要它)主音量(当前的音量)声音的输出源,而不是声音,适用于xp / vista / 7?

如果没有,如何为xp,vista,七来做(我会写我的通用包装器)

我需要显示一个条(进度条)以显示如何设置当前音量(winform控件)

修改

我找到了一个有用的链接,我正在测试它,但我不会检查答案'直到我得到一些有用的东西 http://www.codeproject.com/KB/audio-video/mixerSetControlDetails.aspx?display=Print

编辑2:

重要的是:以前分析音量的方法在Windows Vista或更高版本中无效,请改用此lib: http://www.codeproject.com/KB/vista/CoreAudio.aspx

我不知道是否真的有效,因为我目前没有vista / 7

1 个答案:

答案 0 :(得分:1)

mixerGetNumDevs和相关API是否有帮助? (您必须使用mixerGetLineControls等查看所有设备,并查看哪个MIXERCONTROL说出“Volume”作为名称;还有一种方法可以检查该标志。这就是您正在寻找的那个为。)


编辑:

这是我的一些旧代码的片段;我不认为它编译,并且它不是很好的代码(我只需要完成工作),但它应该有用:

MMRESULT mmResult = mixerOpen(&hMixer, 0, (DWORD_PTR)hWnd, NULL, CALLBACK_WINDOW | MIXER_OBJECTF_MIXER);
if (MMSYSERR_NOERROR == mmResult)
{
    MIXERLINE mxl;
    mxl.cbStruct = sizeof(MIXERLINE);
    mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
    MMRESULT mmResult = mixerGetLineInfo((HMIXEROBJ)hMixer, &mxl, MIXER_OBJECTF_HMIXER | MIXER_GETLINEINFOF_COMPONENTTYPE);
    if (mmResult == MMSYSERR_NOERROR)
    {
        MIXERLINECONTROLS controls;
        memzero(&controls, sizeof(controls));
        controls.cbStruct = sizeof(controls);
        controls.cControls = 1;
        controls.dwLineID = mxl.dwLineID;
        controls.cbmxctrl = sizeof(MIXERCONTROL); //one element only!!
        MIXERCONTROL controlsArray[2]; //First element: volume, second element: mixer
        memzero(&controlsArray, sizeof(controlsArray));
        for (int i = 0; i < sizeof(controlsArray) / sizeof(*controlsArray); i++) { controlsArray[0].cbStruct = sizeof(controlsArray[0]); }

        controls.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
        controls.pamxctrl = &controlsArray[0]; //NOTE: this is ZERO
        mmResult = mixerGetLineControls((HMIXEROBJ)hMixer, &controls, MIXER_GETLINECONTROLSF_ONEBYTYPE | MIXER_OBJECTF_HMIXER);
        if (mmResult == MMSYSERR_NOERROR)
        {
            controls.dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
            controls.pamxctrl = &controlsArray[1]; //NOTE: this is ONE
            mmResult = mixerGetLineControls((HMIXEROBJ)hMixer, &controls, MIXER_GETLINECONTROLSF_ONEBYTYPE | MIXER_OBJECTF_HMIXER);
            if (mmResult == MMSYSERR_NOERROR)
            {
                bool isVolume = controls[0].dwControlID == (DWORD)lParam;
                bool isMute = controls[1].dwControlID == (DWORD)lParam;
                if (isVolume | isMute)
                {
                    MIXERCONTROLDETAILS details;
                    memzero(&details, sizeof(details));
                    details.cbStruct = sizeof(details);
                    details.cChannels = 1;
                    details.dwControlID = (DWORD)lParam;
                    MIXERCONTROLDETAILS_UNSIGNED controlDetail;
                    memzero(&controlDetail, sizeof(controlDetail));
                    details.paDetails = &controlDetail;
                    details.cbDetails = sizeof(controlDetail);
                    MMRESULT mmResult = mixerGetControlDetails((HMIXEROBJ)hMixer, &details, MIXER_GETCONTROLDETAILSF_VALUE | MIXER_OBJECTF_HMIXER);
                    if (mmResult == MMSYSERR_NOERROR)
                    {
                    }
                }
            }
        }
    }
}