1. 程式人生 > >Core Audio APIs 給喇叭、麥克風靜音

Core Audio APIs 給喇叭、麥克風靜音

有的時候我們需要檢查系統預設喇叭,麥克風的靜音狀態以及設定靜音

該怎麼做呢?
請參考以下程式碼


#include <Mmdeviceapi.h>
#include <Endpointvolume.h>
#include <Audioclient.h>
#include <Audiopolicy.h>

const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
const IID IID_IAudioEndpointVolume = __uuidof(IAudioEndpointVolume);
const IID IID_ISimpleAudioVolume = __uuidof(ISimpleAudioVolume);
const IID IID_IAudioSessionManager = __uuidof(IAudioSessionManager);

CComPtr<IMMDeviceEnumerator > m_pIMMEnumerator;   
	CComPtr<IAudioEndpointVolume> m_pRenderEndptVol;   
	CComPtr<ISimpleAudioVolume>   m_pRenderSimpleVol;
	
	CComPtr<IMMDevice> pIMMDeivce = NULL;
	HRESULT hr = S_OK;
	hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, 
		IID_IMMDeviceEnumerator, (void**)&m_pIMMEnumerator);
	//Audio
	//hr = m_pIMMEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pIMMDeivce);

	//Microphone
	hr = m_pIMMEnumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &pIMMDeivce);

	hr = pIMMDeivce->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (void**)&m_pRenderEndptVol);

BOOL bMute = FALSE;
	hr = m_pRenderEndptVol->GetMute(&bMute);
	bMute = TRUE;
	hr = m_pRenderEndptVol->SetMute(bMute, NULL);