使用例子如下:

CAudioMgr AudioMgr;
AudioMgr.SetProcessMute(GetCurrentProcessId());

H文件的内容如下:

#pragma once

#include <Audiopolicy.h>

class CAudioMgr
{
public:
CAudioMgr();
~CAudioMgr(); public:
HRESULT GetHResult() const { return m_hRes; }
BOOL SetProcessMute(const CString& strProcName);
BOOL SetProcessMute(DWORD Pid); private:
BOOL __GetAudioSessionMgr2(); private:
HRESULT m_hRes;
IAudioSessionManager2* m_lpAudioSessionMgr;
};

CPP文件内容如下:

#include "stdafx.h"
#include <mmdeviceapi.h>
#include <Psapi.h>
#include "AudioManager.h" #pragma comment(lib, "Psapi.lib") CAudioMgr::CAudioMgr()
: m_hRes( ERROR_SUCCESS )
, m_lpAudioSessionMgr( NULL )
{
::CoInitialize(NULL);
} CAudioMgr::~CAudioMgr()
{
::CoUninitialize();
} BOOL CAudioMgr::SetProcessMute(DWORD Pid)
{
if ( !this->__GetAudioSessionMgr2() || m_lpAudioSessionMgr == NULL )
{
return FALSE;
} CComPtr<IAudioSessionEnumerator> pAudioSessionEnumerator;
m_hRes = m_lpAudioSessionMgr->GetSessionEnumerator(&pAudioSessionEnumerator);
if ( FAILED(m_hRes) || pAudioSessionEnumerator == NULL )
{
return FALSE;
} int nCount = ;
m_hRes = pAudioSessionEnumerator->GetCount(&nCount); for ( int i = ; i < nCount; ++i )
{
CComPtr<IAudioSessionControl> pAudioSessionControl;
m_hRes = pAudioSessionEnumerator->GetSession(i, &pAudioSessionControl);
if ( FAILED(m_hRes) || pAudioSessionControl == NULL )
{
continue;
} CComQIPtr<IAudioSessionControl2> pAudioSessionControl2(pAudioSessionControl);
if ( pAudioSessionControl2 == NULL )
{
continue;
} DWORD dwPid = ;
m_hRes = pAudioSessionControl2->GetProcessId(&dwPid);
if( FAILED(m_hRes) )
{
continue;
} if ( dwPid == Pid )
{
CComQIPtr<ISimpleAudioVolume> pSimpleAudioVolume(pAudioSessionControl2);
if ( pSimpleAudioVolume == NULL )
{
continue;
} m_hRes = pSimpleAudioVolume->SetMute(TRUE, NULL);
break;
}
} return SUCCEEDED(m_hRes);
} BOOL CAudioMgr::SetProcessMute(const CString& strProcName)
{
if ( !this->__GetAudioSessionMgr2() || m_lpAudioSessionMgr == NULL )
{
return FALSE;
} CComPtr<IAudioSessionEnumerator> pAudioSessionEnumerator;
m_hRes = m_lpAudioSessionMgr->GetSessionEnumerator(&pAudioSessionEnumerator);
if ( FAILED(m_hRes) || pAudioSessionEnumerator == NULL )
{
return FALSE;
} int nCount = ;
m_hRes = pAudioSessionEnumerator->GetCount(&nCount); for ( int i = ; i < nCount; ++i )
{
CComPtr<IAudioSessionControl> pAudioSessionControl;
m_hRes = pAudioSessionEnumerator->GetSession(i, &pAudioSessionControl);
if ( FAILED(m_hRes) || pAudioSessionControl == NULL )
{
continue;
} CComQIPtr<IAudioSessionControl2> pAudioSessionControl2(pAudioSessionControl);
if ( pAudioSessionControl2 == NULL )
{
continue;
} DWORD dwPid = ;
m_hRes = pAudioSessionControl2->GetProcessId(&dwPid);
if( FAILED(m_hRes) )
{
continue;
} CHandle hProcess;
hProcess.Attach(OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid));
if ( hProcess == NULL )
{
continue;
} TCHAR szName[MAX_PATH] = { };
::GetModuleBaseName(hProcess, NULL, szName, MAX_PATH);
if ( strProcName.CompareNoCase(szName) == )
{
CComQIPtr<ISimpleAudioVolume> pSimpleAudioVolume(pAudioSessionControl2);
if ( pSimpleAudioVolume == NULL )
{
continue;
}
m_hRes = pSimpleAudioVolume->SetMute(TRUE, NULL);
break;
}
} return SUCCEEDED(m_hRes);
} BOOL CAudioMgr::__GetAudioSessionMgr2()
{
if ( m_lpAudioSessionMgr == NULL )
{
CComPtr<IMMDeviceEnumerator> pMMDeviceEnumerator; m_hRes = pMMDeviceEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL);
if ( FAILED(m_hRes) || (pMMDeviceEnumerator == NULL) )
{
return FALSE;
} CComPtr<IMMDevice> pDefaultDevice;
m_hRes = pMMDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDefaultDevice);
if( FAILED(m_hRes) || pDefaultDevice == NULL )
{
return FALSE;
} m_hRes = pDefaultDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (void**)&m_lpAudioSessionMgr);
if ( FAILED(m_hRes) || (m_lpAudioSessionMgr == NULL) )
{
return FALSE;
}
} return TRUE;
}
05-11 22:20