VC调节系统音量代码

**

VC调节系统音量代码

mark as [lconductor_001]
使用IAudioEndpointVolume控制SPeakers的方式需要win7的支持,如果用于XP系统的音量控制,可使用混音器MIXER 。

1、IAudioEndpointVolume方式

http://msdn.microsoft.com/en-us/library/dd370839(v=VS.85).aspx
**

#include "StdAfx.h"
#include "SysVolumnCtrl.h"
#include "Audioclient.h"
#include "endpointvolume.h"
#include "mmdeviceapi.h"
#include "audiopolicy.h"

CSysVolumnCtrl::CSysVolumnCtrl(void)
{
	CoInitialize(NULL);
}


CSysVolumnCtrl::~CSysVolumnCtrl(void)
{
	CoUninitialize();
}

long CSysVolumnCtrl::GetVolumeScalar(float& fLevel)
{
	IMMDeviceEnumerator *pEnumerator = NULL;
	IMMDevice *pDevice = NULL;
	HRESULT _hrStatus = S_OK;
	IAudioEndpointVolume *endpointVolume = NULL;
	do 
	{
		_hrStatus = CoCreateInstance(__uuidof(MMDeviceEnumerator),
			NULL, CLSCTX_INPROC_SERVER,
			__uuidof(IMMDeviceEnumerator),
			(void**)&pEnumerator);
		if (S_OK != _hrStatus)
		{
			break;
		}
		_hrStatus = pEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia,&pDevice);
		if (S_OK != _hrStatus)
		{
			break;
		}
		_hrStatus = pDevice->Activate(__uuidof(IAudioEndpointVolume), 
			CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
		if (S_OK != _hrStatus)
		{
			break;
		}
		_hrStatus = endpointVolume->GetMasterVolumeLevelScalar(&fLevel);
	} while (0);
	if (NULL != endpointVolume)
	{
		endpointVolume->Release();
	}
	if (NULL != pDevice)
	{
		pDevice->Release();
	}
	if (NULL != pEnumerator)
	{
		pEnumerator->Release();
	}
	return _hrStatus;
}

long CSysVolumnCtrl::SetVolumeScalar(float fLevel)
{
	IMMDeviceEnumerator *pEnumerator = NULL;
	IMMDevice *pDevice = NULL;
	HRESULT _hrStatus = S_OK;
	IAudioEndpointVolume *endpointVolume = NULL;
	do 
	{
		_hrStatus = CoCreateInstance(__uuidof(MMDeviceEnumerator),
			NULL, CLSCTX_INPROC_SERVER,
			__uuidof(IMMDeviceEnumerator),
			(void**)&pEnumerator);
		if (S_OK != _hrStatus)
		{
			break;
		}
		_hrStatus = pEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia,&pDevice);
		if (S_OK != _hrStatus)
		{
			break;
		}
		_hrStatus = pDevice->Activate(__uuidof(IAudioEndpointVolume), 
			CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
		if (S_OK != _hrStatus)
		{
			break;
		}
		_hrStatus = endpointVolume->SetMasterVolumeLevelScalar(fLevel, NULL);
	} while (0);
	if (NULL != endpointVolume)
	{
		endpointVolume->Release();
	}
	if (NULL != pDevice)
	{
		pDevice->Release();
	}
	if (NULL != pEnumerator)
	{
		pEnumerator->Release();
	}
	return _hrStatus;
}

整理摘自 http://blog.csdn.net/mengzhishen/article/details/8281726

2、 混音器MIXER 控制 XP 音量

// SysVolumeControl.cpp: implementation of the CSysVolumeControl class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "e.h"
#include "SysVolumeControl.h"
   

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSysVolumeControl::CSysVolumeControl()
{

}

CSysVolumeControl::~CSysVolumeControl()
{

}
    
    
bool   CSysVolumeControl::GetVolumeControl(HMIXER   hmixer   ,long   componentType,long   ctrlType,MIXERCONTROL*   mxc)   
{   
	MIXERLINECONTROLS   mxlc;   
	MIXERLINE   mxl;   
	mxl.cbStruct   =   sizeof(mxl);   
	mxl.dwComponentType   =   componentType; 
	MMRESULT mmResult = mixerGetLineInfo((HMIXEROBJ)hmixer,   &mxl, /*MIXER_OBJECTF_HMIXER |*/MIXER_GETLINEINFOF_COMPONENTTYPE);
	if(!mmResult)   
	{   
		mxlc.cbStruct   =   sizeof(mxlc);   
		mxlc.dwLineID   =   mxl.dwLineID;   
		mxlc.dwControlType   =   ctrlType;   
		mxlc.cControls   =   1;   
		mxlc.cbmxctrl   =   sizeof(MIXERCONTROL);   
		mxlc.pamxctrl   =   mxc;   
		if(mixerGetLineControls((HMIXEROBJ)hmixer,&mxlc,/*MIXER_OBJECTF_HMIXER |*/MIXER_GETLINECONTROLSF_ONEBYTYPE))   
			return   0;   
		else   
			return   1;   
	}     
	return   0;   
}     
//---------------------------------------------------------------------------     
long   CSysVolumeControl::GetMuteValue(HMIXER   hmixer   ,MIXERCONTROL   *mxc)   
{   
	MIXERCONTROLDETAILS   mxcd;     
	MIXERCONTROLDETAILS_BOOLEAN   mxcdMute;   
	mxcd.hwndOwner   =   0;     
	mxcd.cbStruct   =   sizeof(mxcd);     
	mxcd.dwControlID   =   mxc->dwControlID;     
	mxcd.cbDetails   =   sizeof(mxcdMute);   
	mxcd.paDetails   =   &mxcdMute;     
	mxcd.cChannels   =   1;   
	mxcd.cMultipleItems   =   0;     
	if   (mixerGetControlDetails((HMIXEROBJ)hmixer,   &mxcd,MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE))     
		return   -1;     
	return   mxcdMute.fValue;   
}     

//---------------------------------------------------------------------------     
unsigned   CSysVolumeControl::GetVolumeValue(HMIXER   hmixer   ,MIXERCONTROL   *mxc)   
{     
	MIXERCONTROLDETAILS   mxcd;   
	MIXERCONTROLDETAILS_UNSIGNED   vol;   vol.dwValue=0;     
	mxcd.hwndOwner   =   0;   
	mxcd.cbStruct   =   sizeof(mxcd);     
	mxcd.dwControlID   =   mxc->dwControlID;     
	mxcd.cbDetails   =   sizeof(vol);     
	mxcd.paDetails   =   &vol;   
	mxcd.cChannels   =   1;   
	if(mixerGetControlDetails((HMIXEROBJ)hmixer,   &mxcd,   MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE))   
		return   -1;     
	return   vol.dwValue;     
}     

//---------------------------------------------------------------------------   
bool   CSysVolumeControl::SetMuteValue(HMIXER   hmixer   ,MIXERCONTROL   *mxc,   bool   mute)   
{     
	MIXERCONTROLDETAILS   mxcd;     
	MIXERCONTROLDETAILS_BOOLEAN   mxcdMute;mxcdMute.fValue=mute;     
	mxcd.hwndOwner   =   0;   
	mxcd.dwControlID   =   mxc->dwControlID;     
	mxcd.cbStruct   =   sizeof(mxcd);   
	mxcd.cbDetails   =   sizeof(mxcdMute);     
	mxcd.paDetails   =   &mxcdMute;     
	mxcd.cChannels   =   1;     
	mxcd.cMultipleItems   =   0;   
	if   (mixerSetControlDetails((HMIXEROBJ)hmixer,   &mxcd,   MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE))     
		return   0;   
	return   1;     
}     

//---------------------------------------------------------------------------   

bool   CSysVolumeControl::SetVolumeValue(HMIXER   hmixer   ,MIXERCONTROL   *mxc,   long   volume)   
{     
	MIXERCONTROLDETAILS   mxcd;     
	MIXERCONTROLDETAILS_UNSIGNED   vol;vol.dwValue   =   volume;     
	mxcd.hwndOwner   =   0;   
	mxcd.dwControlID   =   mxc->dwControlID;     
	mxcd.cbStruct   =   sizeof(mxcd);   
	mxcd.cbDetails   =   sizeof(vol);   
	mxcd.paDetails   =   &vol;   
	mxcd.cChannels   =   1;   
	if(mixerSetControlDetails((HMIXEROBJ)hmixer,   &mxcd,   MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE))   
		return   0;   
	return   1;   
}     


//---------------------------------------------------------------------------   
unsigned   WINAPI   CSysVolumeControl::GetVolume(int   dev)//得到设备的音量dev=0主音量,1WAVE   ,2MIDI   ,3   LINE   IN   
{   
	long   device;unsigned   rt=0;     
	MIXERCONTROL   volCtrl;     
	HMIXER   hmixer;     
	switch   (dev)   
	{   
	case   1:     
		device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;   break;     
	case   2:     
		device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;   break;     
	case   3:   
	//   device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;   break;   //   cd   音量     
	//   device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;   break;   //麦克风音量     
	//   device=MIXERLINE_COMPONENTTYPE_SRC_LINE;   break;   //PC   扬声器音量     
		device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;   break;     
	case   4:   
		device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;break;   
	default:   
		device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;     
	}   
	UINT iNumDevs = mixerGetNumDevs();
	if(mixerOpen(&hmixer,   0,   0,   0,   0))   return   0;   
	if(!GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_VOLUME,&volCtrl))     
		return   0;   
	rt=GetVolumeValue(hmixer,&volCtrl)*100/volCtrl.Bounds.lMaximum;   
	mixerClose(hmixer);   
	return   rt;   

}   

//---------------------------------------------------------------------------   

bool   WINAPI   CSysVolumeControl::SetVolume(long   dev,long   vol)//设置设备的音量   
{   
//   dev   =0,1,2   分别表示主音量,波形,MIDI   ,LINE   IN   
//   vol=0-100   表示音量的大小   ,   设置与返回音量的值用的是百分比,即音量从0   -   100,而不是设备的绝对值   
//   retrun   false   表示设置音量的大小的操作不成功   
//   retrun   true   表示设置音量的大小的操作成功   

	long   device;   
	bool   rc=false;   
	MIXERCONTROL   volCtrl;   
	HMIXER   hmixer;   
	switch   (dev)   
	{   
		case   1:   
			device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;   break;   
		case   2:   
			device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;   break;   
		case   3:   
			device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;   break;   
		case   4:   
			device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;break;   
		default:     
			device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;     
	}     

	if(mixerOpen(&hmixer,   0,   0,   0,   0))   return   0;   

	if(GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_VOLUME,&volCtrl))     
	{     
		vol=vol*volCtrl.Bounds.lMaximum/100;     
		if(SetVolumeValue(hmixer,&volCtrl,vol))   
		rc=true;   
	}     
	mixerClose(hmixer);     
	return   rc;     
}     

//---------------------------------------------------------------------------   

bool   WINAPI   CSysVolumeControl::SetMute(long   dev,bool   vol)//设置设备静音   
{   
//   dev   =0,1,2   分别表示主音量,波形,MIDI   ,LINE   IN   
//   vol=0,1   分别表示取消静音,设置静音   
//   retrun   false   表示取消或设置静音操作不成功   
//   retrun   true   表示取消或设置静音操作成功   

	long   device;   
	bool   rc=false;   
	MIXERCONTROL   volCtrl;   
	HMIXER   hmixer;   
	switch   (dev)   
	{   
	case   1:   
		device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;   break;   
	case   2:   
		device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;   break;   
	case   3:   
		device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;   break;     
	case   4:   
		device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;break;   
	default:     
		device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;   
	}     

	if(mixerOpen(&hmixer,   0,   0,   0,   0))   return   0;     
	if(GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_MUTE,&volCtrl))     
		if(SetMuteValue(hmixer,&volCtrl,(bool)vol))     
			rc=true;   
	mixerClose(hmixer);     
	return   rc;   
}     

//---------------------------------------------------------------------------   

bool   WINAPI   CSysVolumeControl::GetMute(long   dev)//检查设备是否静音   
{   
//dev   =0,1,2   分别表示主音量,波形,MIDI   ,LINE   IN   
//   retrun   false   表示没有静音   
//   retrun   true   表示静音   
	long   device;   
	bool   rc=false;   
	MIXERCONTROL   volCtrl;   
	HMIXER   hmixer;   
	switch   (dev)   
	{   
	case   1:   
		device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;   break;   
	case   2:   
		device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;   break;   
	case   3:   
		device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;   break;   
	case   4:   
		device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;break;   
	default:   
		device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;   
	}     

	if(mixerOpen(&hmixer,   0,   0,   0,   0))   return   0;     

	if(GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_MUTE,&volCtrl))   
		rc=GetMuteValue(hmixer,&volCtrl);   
	mixerClose(hmixer);   
	return   rc;   

}

参考 https://www.xuebuyuan.com/2051646.html

你可能感兴趣的:(日常记录)