win7下!将本进程设置为静音!而且在合成器中可以看到本进程是静音的图标!但是仍然能听到声音!为什么?是不是后续还有什么步骤?

jing0090 2012-04-24 04:48:32
同样设置音量为零的话,也是如此!

BOOL SetMute(BOOL bMute)
{
HMIXER hMixer;
MIXERCONTROL mxc;
MIXERLINE mxl;
MIXERLINECONTROLS mxlc;
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_SIGNED volStruct;
MMRESULT mmr;

//Sound Setting
mmr = mixerOpen(&hMixer, 0, 0, 0, 0);
if (mmr != MMSYSERR_NOERROR) return FALSE;

// 初始化MIXERLINE结构体.
ZeroMemory(&mxl, sizeof(MIXERLINE));
mxl.cbStruct = sizeof(MIXERLINE);

// 指出需要获取的通道,扬声器用MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;

mmr = mixerGetLineInfo((HMIXEROBJ)hMixer, &mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);
if (mmr != MMSYSERR_NOERROR) return FALSE;

// 取得控制器.
ZeroMemory(&mxlc, sizeof(MIXERLINECONTROLS));
mxlc.cbStruct = sizeof(MIXERLINECONTROLS);
mxlc.dwLineID = mxl.dwLineID;
mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
mxlc.cControls = 1;
mxlc.cbmxctrl = sizeof(mxc);
mxlc.pamxctrl = &mxc;
//mxc.cMultipleItems=0;

ZeroMemory(&mxc, sizeof(MIXERCONTROL));
mxc.cbStruct = sizeof(MIXERCONTROL);
mmr = mixerGetLineControls((HMIXEROBJ)hMixer, &mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE);
if (mmr != MMSYSERR_NOERROR) return FALSE;

// 初始化MIXERCONTROLDETAILS结构体
ZeroMemory(&mxcd, sizeof(MIXERCONTROLDETAILS));
mxcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_SIGNED);
mxcd.dwControlID = mxc.dwControlID;
mxcd.paDetails = &volStruct;
mxcd.cChannels = 1;

// 获得音量值
volStruct.lValue = bMute;
mmr = mixerSetControlDetails((HMIXEROBJ)hMixer, &mxcd, MIXER_SETCONTROLDETAILSF_VALUE);

mixerClose(hMixer);
return TRUE;
}
...全文
454 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq102573012 2013-04-11
  • 打赏
  • 举报
回复
最近被这个问题搞得头大了,不知道楼主解决了吗
gdljf 2012-10-25
  • 打赏
  • 举报
回复
//得到设备硬件ID (设备管理器可以看到的硬件ID)
bool CCoreAudioVolume::GetDeviceDsc(IMMDevice *pDevice,wchar_t* DeviceDsc)
{
HRESULT hr;
IPropertyStore *pStore;
hr = pDevice->OpenPropertyStore(STGM_READ, &pStore);
if (SUCCEEDED(hr))
{
PROPERTYKEY Drvidkey ={0xb3f8fa53, 0x0004, 0x438e, 0x90, 0x03, 0x51, 0xa4, 0x6e, 0x13, 0x9b, 0xfc, 2};
PROPVARIANT pDrvidkey;
PropVariantInit(&pDrvidkey);
hr = pStore->GetValue(Drvidkey , &pDrvidkey);
if (SUCCEEDED(hr))
{
wcscpy(DeviceDsc,pDrvidkey.pwszVal);
PropVariantClear(&pDrvidkey);
pStore->Release();
return true;
}
pStore->Release();
}
return false;
}

// 验证设备是否指定设备
bool CCoreAudioVolume::VerifyDev(IMMDevice *pDevice,EDataFlow dataFlow)
{
wchar_t DeviceDsc[255];
if (GetDeviceDsc(pDevice,DeviceDsc))
{
// 这里省略判断具体设备的 匹配硬件 如 HDAUDIO\FUNC_01&VEN_10EC&DEV_0888&SUBSYS_14627514&REV_1000
return true;
}
return false;
}

// 获取设备音量
int CCoreAudioVolume::GetDevicePlayVol(void)
{
IMMDeviceEnumerator* pEnumerator;
IMMDeviceCollection* pCollection = NULL;
IMMDevice *pDevice = NULL;
IAudioEndpointVolume *pVolumeAPI=NULL;
UINT deviceCount = 0;
HRESULT hr;
float fVolume = -1;

CoInitializeEx( NULL , COINIT_MULTITHREADED );
//实例化 MMDeviceEnumerator 枚举器
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),(void**)&pEnumerator);
if (hr != S_OK)
{
goto FreeEnumerator;
}
// 枚举 设备到设备容器 eRander:放音设备,DEVICE_STATE_ACTIVE 为当前已激活的设备,禁用和无连接的用其他状态参数
hr = pEnumerator->EnumAudioEndpoints( eRender , DEVICE_STATE_ACTIVE , &pCollection );
if (hr != S_OK)
{
goto FreeCollection;
}
// 设备容器里的总数
hr = pCollection->GetCount(&deviceCount);
if (hr != S_OK)
{
goto FreeCollection;
}

for (UINT dev=0; dev<deviceCount; dev++)
{
pDevice = NULL;
hr = pCollection->Item(dev,&pDevice);
if (hr == S_OK)
{
if (VerifyDev(pDevice,eRender))
{ // 用 pDevice 的 Activate 方法初始一个 IAudioEndpointVolume 接口
hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),CLSCTX_ALL,NULL,(void **)(&pVolumeAPI));
// 使用 IAudioEndpintVolume 的方法获取音量,设置音量,设置静音等
hr = pVolumeAPI->GetMasterVolumeLevelScalar(&fVolume);
break;
}
}
}

FreeCollection:
SAFE_RELEASE(pCollection);
FreeEnumerator:
SAFE_RELEASE(pEnumerator);
CoUninitialize();
if (fVolume > 0)
return fVolume*100;
else
return fVolume;

return 0;
}

//设置应用程序静音
bool CCoreAudioVolume::SetMute(BOOL bMute)
{
//IAudioSessionControl *pAudioSection = NULL;
ISimpleAudioVolume *pAudioVolume = NULL;
IAudioSessionManager *pManager = NULL;
IMMDevice *pDevice = NULL;
HRESULT hr;

std::vector<IMMDevice*>::iterator iter = m_vArrayDevice.begin();
for(; iter != m_vArrayDevice.end(); iter++)
{
pDevice = *iter;
ATLASSERT(pDevice != NULL);

hr = pDevice->Activate(__uuidof(IAudioSessionManager),
CLSCTX_INPROC_SERVER, NULL,
(void**)&pManager);

if(FAILED(hr)) continue;

//hr = pManager->GetAudioSessionControl(NULL, 0, &pAudioSection);

hr = pManager->GetSimpleAudioVolume(NULL, 0, &pAudioVolume);

if(SUCCEEDED(hr))
{
pAudioVolume->SetMute(bMute, &GUID_NULL);
}
else
{
ATLASSERT(FALSE);
}
}

SAFE_RELEASE(pManager);
SAFE_RELEASE(pAudioVolume);

return true;
}

HRESULT CCoreAudioVolume::EnableSound(BOOL bEnable)
{
m_bEnableSound = bEnable;

bool bResult = CCoreAudioVolume::SetMute(!bEnable);

return bResult ? S_OK : E_FAIL;
}

HRESULT CCoreAudioVolume::Initlialize(BOOL bEnableSound)
{
m_bEnableSound = bEnableSound;

CoInitializeEx( NULL , COINIT_MULTITHREADED );

HRESULT hr;
//实例化 MMDeviceEnumerator 枚举器
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),(void**)&m_pEnumerator);
if(FAILED(hr))
{
return E_FAIL;
}

// 枚举 设备到设备容器 eRander:放音设备,DEVICE_STATE_ACTIVE 为当前已激活的设备,禁用和无连接的用其他状态参数
hr = m_pEnumerator->EnumAudioEndpoints( eRender , DEVICE_STATE_ACTIVE , &m_pCollection );
if (hr != S_OK)
{
return E_FAIL;
}

// 设备容器里的总数
hr = m_pCollection->GetCount(&m_nDevCount);
if (hr != S_OK)
{
return E_FAIL;
}

IMMDevice* pDevice = NULL;

for (UINT dev=0; dev<m_nDevCount; dev++)
{
pDevice = NULL;
hr = m_pCollection->Item(dev,&pDevice);
if (hr == S_OK)
{
if (VerifyDev(pDevice,eRender))
{
m_vArrayDevice.push_back(pDevice);
}
}
}

return S_OK;
}

HRESULT CCoreAudioVolume::Uninitialize()
{
std::vector<IMMDevice*>::iterator iter = m_vArrayDevice.begin();
for(; iter != m_vArrayDevice.end(); iter++)
{
SAFE_RELEASE(*iter);
}
SAFE_RELEASE(m_pCollection);
SAFE_RELEASE(m_pEnumerator);

CoUninitialize();
m_vArrayDevice.clear();

return S_OK;
}
gdljf 2012-10-25
  • 打赏
  • 举报
回复
WIN7程序静音

使用的时候依次 1:initialize 2:EnableSound() 3:Uninitialize , 完成


头文件 .h

[cpp] view plaincopyprint?#pragma once

#include <mmdeviceapi.h>
#include <Endpointvolume.h>
#include <Audioclient.h>
#include <Audiopolicy.h>
#include <vector>

class CCoreAudioVolume
{
public:
static HRESULT EnableSound(BOOL bEnable);

static HRESULT Initlialize(BOOL bEnableSound);

static HRESULT Uninitialize();

private:
static bool GetDeviceDsc(IMMDevice *pDevice,wchar_t* DeviceDsc);

static bool VerifyDev(IMMDevice *pDevice,EDataFlow dataFlow);

static int GetDevicePlayVol(void);

static bool SetMute(BOOL bMute);

private:
static BOOL m_bEnableSound;
static IMMDeviceEnumerator* m_pEnumerator;
static IMMDeviceCollection* m_pCollection;
// static IAudioEndpointVolume* pVolumeAPI=NULL;
static UINT m_nDevCount;
static std::vector<IMMDevice*> m_vArrayDevice;
};

#pragma once

#include <mmdeviceapi.h>
#include <Endpointvolume.h>
#include <Audioclient.h>
#include <Audiopolicy.h>
#include <vector>

class CCoreAudioVolume
{
public:
static HRESULT EnableSound(BOOL bEnable);

static HRESULT Initlialize(BOOL bEnableSound);

static HRESULT Uninitialize();

private:
static bool GetDeviceDsc(IMMDevice *pDevice,wchar_t* DeviceDsc);

static bool VerifyDev(IMMDevice *pDevice,EDataFlow dataFlow);

static int GetDevicePlayVol(void);

static bool SetMute(BOOL bMute);

private:
static BOOL m_bEnableSound;
static IMMDeviceEnumerator* m_pEnumerator;
static IMMDeviceCollection* m_pCollection;
// static IAudioEndpointVolume* pVolumeAPI=NULL;
static UINT m_nDevCount;
static std::vector<IMMDevice*> m_vArrayDevice;
};

cpp文件 .cpp





[cpp] view plaincopyprint?#include "StdAfx.h"
#include ".\coreaudiovolume.h"

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

BOOL CCoreAudioVolume::m_bEnableSound = FALSE;
IMMDeviceEnumerator* CCoreAudioVolume::m_pEnumerator = NULL;
IMMDeviceCollection* CCoreAudioVolume::m_pCollection = NULL;
UINT CCoreAudioVolume::m_nDevCount = 0;
std::vector<IMMDevice*> CCoreAudioVolume::m_vArrayDevice;


//得到设备硬件ID (设备管理器可以看到的硬件ID)
bool CCoreAudioVolume::GetDeviceDsc(IMMDevice *pDevice,wchar_t* DeviceDsc)
{
HRESULT hr;
IPropertyStore *pStore;
hr = pDevice->OpenPropertyStore(STGM_READ, &pStore);
if (SUCCEEDED(hr))
{
PROPERTYKEY Drvidkey ={0xb3f8fa53, 0x0004, 0x438e, 0x90, 0x03, 0x51, 0xa4, 0x6e, 0x13, 0x9b, 0xfc, 2};
PROPVARIANT pDrvidkey;
PropVariantInit(&pDrvidkey);
hr = pStore->GetValue(Drvidkey , &pDrvidkey);
if (SUCCEEDED(hr))
{
wcscpy(DeviceDsc,pDrvidkey.pwszVal);
PropVariantClear(&pDrvidkey);
pStore->Release();
return true;
}
pStore->Release();
}
return false;
}

// 验证设备是否指定设备
bool CCoreAudioVolume::VerifyDev(IMMDevice *pDevice,EDataFlow dataFlow)
{
wchar_t DeviceDsc[255];
if (GetDeviceDsc(pDevice,DeviceDsc))
{
// 这里省略判断具体设备的 匹配硬件 如 HDAUDIO\FUNC_01&VEN_10EC&DEV_0888&SUBSYS_14627514&REV_1000
return true;
}
return false;
}

// 获取设备音量
int CCoreAudioVolume::GetDevicePlayVol(void)
{
IMMDeviceEnumerator* pEnumerator;
IMMDeviceCollection* pCollection = NULL;
IMMDevice *pDevice = NULL;
IAudioEndpointVolume *pVolumeAPI=NULL;
UINT deviceCount = 0;
HRESULT hr;
float fVolume = -1;

CoInitializeEx( NULL , COINIT_MULTITHREADED );
//实例化 MMDeviceEnumerator 枚举器
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),(void**)&pEnumerator);
if (hr != S_OK)
{
goto FreeEnumerator;
}
// 枚举 设备到设备容器 eRander:放音设备,DEVICE_STATE_ACTIVE 为当前已激活的设备,禁用和无连接的用其他状态参数
hr = pEnumerator->EnumAudioEndpoints( eRender , DEVICE_STATE_ACTIVE , &pCollection );
if (hr != S_OK)
{
goto FreeCollection;
}
// 设备容器里的总数
hr = pCollection->GetCount(&deviceCount);
if (hr != S_OK)
{
goto FreeCollection;
}

for (UINT dev=0; dev<deviceCount; dev++)
{
pDevice = NULL;
hr = pCollection->Item(dev,&pDevice);
if (hr == S_OK)
{
if (VerifyDev(pDevice,eRender))
{ // 用 pDevice 的 Activate 方法初始一个 IAudioEndpointVolume 接口
hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),CLSCTX_ALL,NULL,(void **)(&pVolumeAPI));
// 使用 IAudioEndpintVolume 的方法获取音量,设置音量,设置静音等
hr = pVolumeAPI->GetMasterVolumeLevelScalar(&fVolume);
break;
}
}
}

FreeCollection:
SAFE_RELEASE(pCollection);
FreeEnumerator:
SAFE_RELEASE(pEnumerator);
CoUninitialize();
if (fVolume > 0)
return fVolume*100;
else
return fVolume;

return 0;
}

//设置应用程序静音
bool CCoreAudioVolume::SetMute(BOOL bMute)
{
//IAudioSessionControl *pAudioSection = NULL;
ISimpleAudioVolume *pAudioVolume = NULL;
IAudioSessionManager *pManager = NULL;
IMMDevice *pDevice = NULL;
HRESULT hr;

std::vector<IMMDevice*>::iterator iter = m_vArrayDevice.begin();
for(; iter != m_vArrayDevice.end(); iter++)
{
pDevice = *iter;
ATLASSERT(pDevice != NULL);

hr = pDevice->Activate(__uuidof(IAudioSessionManager),
CLSCTX_INPROC_SERVER, NULL,
(void**)&pManager);

if(FAILED(hr)) continue;

//hr = pManager->GetAudioSessionControl(NULL, 0, &pAudioSection);

hr = pManager->GetSimpleAudioVolume(NULL, 0, &pAudioVolume);

if(SUCCEEDED(hr))
{
pAudioVolume->SetMute(bMute, &GUID_NULL);
}
else
{
ATLASSERT(FALSE);
}
}

SAFE_RELEASE(pManager);
SAFE_RELEASE(pAudioVolume);

return true;
}

HRESULT CCoreAudioVolume::EnableSound(BOOL bEnable)
{
m_bEnableSound = bEnable;

bool bResult = CCoreAudioVolume::SetMute(!bEnable);

return bResult ? S_OK : E_FAIL;
}

HRESULT CCoreAudioVolume::Initlialize(BOOL bEnableSound)
{
m_bEnableSound = bEnableSound;

CoInitializeEx( NULL , COINIT_MULTITHREADED );

HRESULT hr;
//实例化 MMDeviceEnumerator 枚举器
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),(void**)&m_pEnumerator);
if(FAILED(hr))
{
return E_FAIL;
}

// 枚举 设备到设备容器 eRander:放音设备,DEVICE_STATE_ACTIVE 为当前已激活的设备,禁用和无连接的用其他状态参数
hr = m_pEnumerator->EnumAudioEndpoints( eRender , DEVICE_STATE_ACTIVE , &m_pCollection );
if (hr != S_OK)
{
return E_FAIL;
}

// 设备容器里的总数
hr = m_pCollection->GetCount(&m_nDevCount);
if (hr != S_OK)
{
return E_FAIL;
}

IMMDevice* pDevice = NULL;

for (UINT dev=0; dev<m_nDevCount; dev++)
{
pDevice = NULL;
hr = m_pCollection->Item(dev,&pDevice);
if (hr == S_OK)
{
if (VerifyDev(pDevice,eRender))
{
m_vArrayDevice.push_back(pDevice);
}
}
}

return S_OK;
}

HRESULT CCoreAudioVolume::Uninitialize()
{
std::vector<IMMDevice*>::iterator iter = m_vArrayDevice.begin();
for(; iter != m_vArrayDevice.end(); iter++)
{
SAFE_RELEASE(*iter);
}
SAFE_RELEASE(m_pCollection);
SAFE_RELEASE(m_pEnumerator);

CoUninitialize();
m_vArrayDevice.clear();

return S_OK;
}

#include "StdAfx.h"
#include ".\coreaudiovolume.h"

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

BOOL CCoreAudioVolume::m_bEnableSound = FALSE;
IMMDeviceEnumerator* CCoreAudioVolume::m_pEnumerator = NULL;
IMMDeviceCollection* CCoreAudioVolume::m_pCollection = NULL;
UINT CCoreAudioVolume::m_nDevCount = 0;
std::vector<IMMDevice*> CCoreAudioVolume::m_vArrayDevice;




16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧