micphone靜音設置(vista版本以上)

設置麥克風靜音


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

#include "mmdeviceapi.h"
#include <endpointvolume.h>

#pragma comment(lib, "Winmm.lib")

#define SAFE_RELEASE(ptr) \
		{ \
		if (ptr) \
		{ \
			(ptr)->Release(); \
			(ptr) = NULL; \
		} \
		}

#define EXIT_ON_ERROR(hr)  \
        if (FAILED(hr)) { goto Exit; }

int CMixer::GetMuteVista(MixerDeice dev)
{
	EDataFlow device;
	BOOL bState = FALSE;
	HRESULT hr = S_OK;

	IMMDeviceEnumerator *deviceEnumerator = NULL;
	IMMDevice *defaultDevice = NULL;
	IAudioEndpointVolume *endpointVolume = NULL;

	switch (dev)
	{
	case MICROPHONE:  
		device = eCapture; break; //麥克風音量   
	default:
		device = eRender; //PC 揚聲器音量   
	}

	hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
		__uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
	EXIT_ON_ERROR(hr)

	hr = deviceEnumerator->GetDefaultAudioEndpoint(device, eConsole, &defaultDevice);
	EXIT_ON_ERROR(hr)
	
	hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
		CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
	EXIT_ON_ERROR(hr)

	hr = endpointVolume->GetMute(&bState);
	EXIT_ON_ERROR(hr)
	
Exit:

	if (FAILED(hr))
	{
		//This program requires Windows Vista. Or no Audio device is installed
		return -1;
	}
	SAFE_RELEASE(deviceEnumerator)
	SAFE_RELEASE(defaultDevice)
	SAFE_RELEASE(endpointVolume)

	return bState ? 0: 1;

}

BOOL CMixer::SetMuteVista(MixerDeice dev, BOOL vol)
{
	EDataFlow device;
	HRESULT hr = S_OK;
	IMMDeviceEnumerator *deviceEnumerator = NULL;
	IMMDevice *defaultDevice = NULL;
	IAudioEndpointVolume *endpointVolume = NULL;

	switch (dev)
	{
	case MICROPHONE:
		device = eCapture; break; 
	default:
		device = eRender;   
	}

	hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
		__uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
	EXIT_ON_ERROR(hr)

	hr = deviceEnumerator->GetDefaultAudioEndpoint(device, eConsole, &defaultDevice);
	EXIT_ON_ERROR(hr)

	hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
		CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
	EXIT_ON_ERROR(hr)

	hr = endpointVolume->SetMute(vol, NULL);
	EXIT_ON_ERROR(hr)

Exit:
	if (FAILED(hr))		
		return FALSE; //This program requires Windows Vista. Or no Audio device is installed

	SAFE_RELEASE(deviceEnumerator)
	SAFE_RELEASE(defaultDevice)
	SAFE_RELEASE(endpointVolume)

	return TRUE;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章