头文件包含:
#include "mmsystem.h"
#pragma comment(lib, "winmm.lib")
BOOL SetMicrPhoneVolume(DWORD dwVolume)
{
BOOL bResult = FALSE;
try
{
int mixerNum = (int)mixerGetNumDevs();
for(int i = 0; i < mixerNum; i ++)
{
DWORD dwSource = 0;
HMIXER m_hMixer = NULL;
MMRESULT mr = NULL;
//打开Mixer设备
if(mixerOpen(&m_hMixer, i, 0, 0, MIXER_OBJECTF_MIXER) != MMSYSERR_NOERROR)
continue;
MIXERLINE MixerLine;
//结构体的大小
MixerLine.cbStruct = sizeof(MixerLine);
//录制设备总线
MixerLine.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
//得到录制总线中的连接数
if(mixerGetLineInfo((HMIXEROBJ)m_hMixer, &MixerLine,
MIXER_OBJECTF_HMIXER | MIXER_GETLINEINFOF_COMPONENTTYPE) != MMSYSERR_NOERROR)
continue;
//将连接数保存
DWORD dwConnections = MixerLine.cConnections;
//准备获取麦克风设备的ID
DWORD dwLineID = 0;
for(DWORD i = 0; i < dwConnections; i++ )
{
//枚举每一个设备,当Source的ID等于当前的迭代记数
MixerLine.dwSource = i;
//根据SourceID获得连接的信息
if(mixerGetLineInfo((HMIXEROBJ)m_hMixer, &MixerLine,
MIXER_OBJECTF_HMIXER | MIXER_GETLINEINFOF_SOURCE) != MMSYSERR_NOERROR)
//判断函数执行错误
break;
//如果当前设备类型是麦克风,则跳出循环。
if(MixerLine.dwComponentType == MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE)
{
dwLineID = MixerLine.dwLineID;
dwSource = MixerLine.dwSource;
break;
}
}
//如果没有找到,返回失败。
if(dwLineID == 0)
continue;
//get line id of destination line
MIXERLINE lineinfo_Dest;
::ZeroMemory(&lineinfo_Dest, sizeof(MIXERLINE));
lineinfo_Dest.cbStruct = sizeof(MIXERLINE);
lineinfo_Dest.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
if(::mixerGetLineInfo(
(HMIXEROBJ)m_hMixer,
&lineinfo_Dest,
MIXER_OBJECTF_HMIXER |
MIXER_GETLINEINFOF_COMPONENTTYPE) != MMSYSERR_NOERROR)
continue;
//get id of specified connector
MIXERLINE lineinfo_Connector;
::ZeroMemory(&lineinfo_Connector, sizeof(MIXERLINE));
lineinfo_Connector.cbStruct = sizeof(MIXERLINE);
lineinfo_Connector.dwDestination = lineinfo_Dest.dwLineID;
lineinfo_Connector.dwSource = dwSource;
if(::mixerGetLineInfo(
(HMIXEROBJ)m_hMixer,
&lineinfo_Connector,
MIXER_GETLINEINFOF_SOURCE) != MMSYSERR_NOERROR)
continue;
MIXERCONTROL mxc;
MIXERLINECONTROLS mxlc;
mxlc.cbStruct = sizeof(MIXERLINECONTROLS);
mxlc.dwLineID = lineinfo_Connector.dwLineID;
mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeof(MIXERCONTROL);
mxlc.pamxctrl = &mxc;
if(::mixerGetLineControls(
reinterpret_cast
&mxlc,
MIXER_OBJECTF_HMIXER|MIXER_GETLINECONTROLSF_ONEBYTYPE)!= MMSYSERR_NOERROR)
continue;
//get volume value range now
MIXERCONTROLDETAILS_UNSIGNED mxcdVolume;
MIXERCONTROLDETAILS mxcd;
mxcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
mxcd.dwControlID = mxc.dwControlID;
mxcd.cChannels = 1;
mxcd.cMultipleItems = 0;
mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
mxcd.paDetails = &mxcdVolume;
if(mixerGetControlDetails(
reinterpret_cast
&mxcd,
MIXER_GETCONTROLDETAILSF_VALUE)!= MMSYSERR_NOERROR)
continue;
//set volume
MIXERCONTROLDETAILS_UNSIGNED mxcdVolume_Set = { mxc.Bounds.dwMaximum * dwVolume / 100 };
MIXERCONTROLDETAILS mxcd_Set;
mxcd_Set.cbStruct = sizeof(MIXERCONTROLDETAILS);
mxcd_Set.dwControlID = mxc.dwControlID;
mxcd_Set.cChannels = 1;
mxcd_Set.cMultipleItems = 0;
mxcd_Set.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
mxcd_Set.paDetails = &mxcdVolume_Set;
if(mixerSetControlDetails(reinterpret_cast
&mxcd_Set,
MIXER_OBJECTF_HMIXER | MIXER_SETCONTROLDETAILSF_VALUE) != MMSYSERR_NOERROR)
continue;
::mixerClose(m_hMixer);
m_hMixer = NULL;
bResult = TRUE;;
}
}
catch(...){ return FALSE; }
return bResult;
}