调节系统音量VC++(适合win和xp系统) 打开静音 增减和减小系统音量

转载 2015年10月22日 13:19:25

#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <mmsystem.h> 
#pragma comment(lib, "winmm.lib") 

 

以下程序为:如果系统处于静音状态 则打开系统音量:

//Windows 系统音量设置

 IMMDevice *device = NULL;

 HRESULT hr = CoInitialize(NULL);
 if (FAILED(hr))
 {
  return FALSE;
 }

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

 if (FAILED(hr))
 {
  // xp 系统调节音量 
  HMIXER hMixer;
  MIXERLINE mixerline;
  MIXERLINECONTROLS mlcs;
  MIXERCONTROL mc;
  MIXERCONTROLDETAILS_BOOLEAN mcdMute;
  MIXERCONTROLDETAILS mcd;
  LONG lvalue;
  try{
   if (MMSYSERR_NOERROR == mixerOpen(&hMixer, 0, 0, 0, 0))
   {
    RtlZeroMemory(&mixerline, sizeof(MIXERLINE));
    mixerline.cbStruct = sizeof(MIXERLINE);
    mixerline.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
    if (MMSYSERR_NOERROR == mixerGetLineInfo((HMIXEROBJ)hMixer, &mixerline, MIXER_GETLINEINFOF_COMPONENTTYPE))
    {
     RtlZeroMemory(&mlcs, sizeof(MIXERLINECONTROLS));
     mlcs.cbStruct = sizeof(MIXERLINECONTROLS);
     mlcs.dwLineID = mixerline.dwLineID;
     mlcs.dwControlType =  MIXERCONTROL_CONTROLTYPE_MUTE;
     mlcs.cControls = 1;
     mlcs.cbmxctrl = sizeof(MIXERCONTROL);
     mlcs.pamxctrl = & mc;
     if (MMSYSERR_NOERROR == mixerGetLineControls((HMIXEROBJ)hMixer, &mlcs, MIXER_GETLINECONTROLSF_ONEBYTYPE))
     {
      RtlZeroMemory(&mcd, sizeof(MIXERCONTROLDETAILS));
      mcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
      mcd.dwControlID = mc.dwControlID;
      mcd.cChannels = 1;
      mcd.cbDetails = sizeof(MIXERCONTROLDETAILS_BOOLEAN);
      mcd.paDetails = &mcdMute;

      if (MMSYSERR_NOERROR == mixerGetControlDetails((HMIXEROBJ)hMixer, &mcd, MIXER_GETCONTROLDETAILSF_VALUE))
      {
       lvalue = mcdMute.fValue;
       if (lvalue != 0 )
       {
        RtlZeroMemory(&mcd, sizeof(MIXERCONTROLDETAILS));
        mcdMute.fValue = FALSE;       //静音设置,0非静音,1静音
        mcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
        mcd.dwControlID = mc.dwControlID;
        mcd.cChannels = 1;
        mcd.cbDetails = sizeof(MIXERCONTROLDETAILS_BOOLEAN);
        mcd.paDetails = &mcdMute;
        mixerSetControlDetails((HMIXEROBJ)hMixer, &mcd, MIXER_SETCONTROLDETAILSF_VALUE);
       }
      }
     }
    }

    MIXERLINE mxl;
    MIXERCONTROL mxc;
    MIXERLINECONTROLS mxlc;
    MIXERCAPS m_mxcaps;

    mixerOpen(&hMixer,0,NULL,
     NULL,MIXER_OBJECTF_MIXER | CALLBACK_WINDOW);
    mixerGetDevCaps((UINT)hMixer, &m_mxcaps, sizeof(MIXERCAPS));
    mxl.cbStruct = sizeof(MIXERLINE);
    mxl.dwComponentType =MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;

    mixerGetLineInfo((HMIXEROBJ)hMixer,&mxl,
     MIXER_OBJECTF_HMIXER|MIXER_GETLINEINFOF_COMPONENTTYPE);


    mxlc.cbStruct = sizeof(mxlc); // 微软用此办法判断版本
    mxlc.dwLineID = mxl.dwLineID; // 上面取得的声卡音频输出线路标识
    // 控制类型为控制音量
    mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
    mxlc.cControls = 1;          // 使用 MIXERCONTROL 结构体变量个数
    mxlc.pamxctrl = &mxc;        // MIXERCONTROL 结构体变量指针
    mxlc.cbmxctrl = sizeof(mxc); // MIXERCONTROL 结构体变量字节大小

    // 取得控制器信息
    mixerGetLineControls((HMIXEROBJ)hMixer,
     &mxlc,
     MIXER_GETLINECONTROLSF_ONEBYTYPE);

    MIXERCONTROLDETAILS_UNSIGNED mxcdVolume;
    MIXERCONTROLDETAILS mxcd;
    mxcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
    mxcd.dwControlID = mxc.dwControlID;
    mxcd.cChannels = 1;
    mxcd.cMultipleItems = 0;
    mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
    mxcd.paDetails = &mxcdVolume;

    mixerGetControlDetails((HMIXEROBJ)hMixer,&mxcd,
     MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE);

    mxcdVolume.dwValue ;
    if (mxcdVolume.dwValue<6553)         // XP 系统 如果打开后系统音量小于10% 则增加到10%        
    {
     mxcdVolume.dwValue = 6553 ;
     mixerSetControlDetails((HMIXEROBJ)hMixer,&mxcd,
      MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE);
    }
   }
   mixerClose(hMixer);
   return FALSE;
  }
  catch(...)
  {
   mixerClose(hMixer);
   return FALSE;
  }
 }
 IMMDevice *defaultDevice = NULL;

 hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
 if (FAILED(hr))
 {
  return FALSE;
 }
 deviceEnumerator->Release();
 deviceEnumerator = NULL;

 IAudioEndpointVolume *endpointVolume;

 hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, reinterpret_cast<void **>(&endpointVolume));
 if (FAILED(hr))
 {
  return FALSE;
 }

 BOOL currentMute;
 hr = endpointVolume->GetMute(&currentMute);
 if (FAILED(hr))
 {
  return FALSE;
 }
 if (currentMute==TRUE)
 {
  hr = endpointVolume->SetMute(FALSE, NULL);
 }

 float currentVolume;
 hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);   //把主音量的水平标量
 if (FAILED(hr))
 {
  return FALSE;
 }
 if(currentVolume<0.1)     //windows  系统 如果系统音量小于10% 则音量增加到40%
 {
  float Volume = 0.400;
  hr = endpointVolume->SetMasterVolumeLevelScalar(Volume,NULL);
 }
 if (FAILED(hr))
 {
  return FALSE;
 }
 return SSN_OK;

 

 

// 以下为系统音量增加1

 

INT IncreaseVolume(__in PSSN_CMD_CALL_DATA pCallData)
{
 IMMDevice *device = NULL;

 HRESULT hr = CoInitialize(NULL);
 if (FAILED(hr))
 {
  return FALSE;
 }

 IMMDeviceEnumerator *deviceEnumerator = NULL;
 hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, 
  __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
 if (FAILED(hr))
 {
  // xp 系统 调节音量
  MIXERLINE mxl;
  MIXERCONTROL mxc;
  MIXERLINECONTROLS mxlc;
  HMIXER m_hMixer;
  MIXERCAPS m_mxcaps;

  mixerOpen(&m_hMixer,0,NULL,
   NULL,MIXER_OBJECTF_MIXER | CALLBACK_WINDOW);
  mixerGetDevCaps((UINT)m_hMixer, &m_mxcaps, sizeof(MIXERCAPS));
  mxl.cbStruct = sizeof(MIXERLINE);
  mxl.dwComponentType =MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;

  mixerGetLineInfo((HMIXEROBJ)m_hMixer,&mxl,
   MIXER_OBJECTF_HMIXER|MIXER_GETLINEINFOF_COMPONENTTYPE);


  mxlc.cbStruct = sizeof(mxlc); // 微软用此办法判断版本
  mxlc.dwLineID = mxl.dwLineID; // 上面取得的声卡音频输出线路标识
  // 控制类型为控制音量
  mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
  mxlc.cControls = 1;          // 使用 MIXERCONTROL 结构体变量个数
  mxlc.pamxctrl = &mxc;        // MIXERCONTROL 结构体变量指针
  mxlc.cbmxctrl = sizeof(mxc); // MIXERCONTROL 结构体变量字节大小

  // 取得控制器信息
  mixerGetLineControls((HMIXEROBJ)m_hMixer,
   &mxlc,
   MIXER_GETLINECONTROLSF_ONEBYTYPE);

  MIXERCONTROLDETAILS_UNSIGNED mxcdVolume;
  MIXERCONTROLDETAILS mxcd;
  mxcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
  mxcd.dwControlID = mxc.dwControlID;
  mxcd.cChannels = 1;
  mxcd.cMultipleItems = 0;
  mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
  mxcd.paDetails = &mxcdVolume;

  mixerGetControlDetails((HMIXEROBJ)m_hMixer,&mxcd,
   MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE);

  mxcdVolume.dwValue = mxcdVolume.dwValue + 655;
  HANDLE hSpeakGroup=SsnBeginSpeakGroup();
  if (mxcdVolume.dwValue<65535)
  {
   INT nVolume = INT( mxcdVolume.dwValue/655.35);
   CString ToSpeak = _T("系统音量增加到");
   CString Volume;
   Volume.Format(_T("%d"), nVolume);
   ToSpeak = ToSpeak + Volume;
   SSN_SPEAK_OPTION SpeakOption;
   if (hSpeakGroup!=INVALID_HANDLE_VALUE)
   {
    SsnAddSpeakString(hSpeakGroup,ToSpeak,&SpeakOption);
   }
   mixerSetControlDetails((HMIXEROBJ)m_hMixer,&mxcd,
    MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE);
  }
  else 
  {
   INT nVolume = 100;
   CString ToSpeak = _T("系统音量增加到");
   CString Volume;
   Volume.Format(_T("%d"), nVolume);
   ToSpeak = ToSpeak + Volume;
   SSN_SPEAK_OPTION SpeakOption;
   if (hSpeakGroup!=INVALID_HANDLE_VALUE)
   {
    SsnAddSpeakString(hSpeakGroup,ToSpeak,&SpeakOption);
   }
   mxcdVolume.dwValue = 65535;
   mixerSetControlDetails((HMIXEROBJ)m_hMixer,&mxcd,
    MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE);
  }
  SsnEndSpeakGroup(hSpeakGroup);  
  return TRUE;
 }
 // windows系统调节音量
 IMMDevice *defaultDevice = NULL;

 hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
 if (FAILED(hr))
 {
  return FALSE;
 }

 deviceEnumerator->Release();
 deviceEnumerator = NULL;

 IAudioEndpointVolume *endpointVolume;

 hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, reinterpret_cast<void **>(&endpointVolume));
 if (FAILED(hr))
 {
  return FALSE;
 }

 hr = endpointVolume->VolumeStepUp(NULL);
 float currentVolume;
 hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
 INT nVolume = INT(currentVolume * 100);
 HANDLE hSpeakGroup=SsnBeginSpeakGroup();
 CString ToSpeak = _T("系统音量增加到");
 CString Volume;
 Volume.Format(_T("%d"), nVolume);
 ToSpeak = ToSpeak + Volume;
 SSN_SPEAK_OPTION SpeakOption;
 if (hSpeakGroup!=INVALID_HANDLE_VALUE)
 {
  SsnAddSpeakString(hSpeakGroup,ToSpeak,&SpeakOption);
 }
 if (FAILED(hr))
 {
  return FALSE;
 }
 if (hr == S_FALSE)
 {
  return FALSE;
 }
 SsnEndSpeakGroup(hSpeakGroup);  
 return TRUE;
}

 

 

// 以下为系统音量减小1

INT DecreaseVolume(__in PSSN_CMD_CALL_DATA pCallData)
{
 IMMDevice *device = NULL;

 HRESULT hr = CoInitialize(NULL);
 if (FAILED(hr))
 {
  return FALSE;
 }

 IMMDeviceEnumerator *deviceEnumerator = NULL;
 hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, 
  __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
 if (FAILED(hr))
 {  
  // xp 系统 调节音量
  MIXERLINE mxl;
  MIXERCONTROL mxc;
  MIXERLINECONTROLS mxlc;
  HMIXER m_hMixer;
  MIXERCAPS m_mxcaps;

  mixerOpen(&m_hMixer,0,NULL,
   NULL,MIXER_OBJECTF_MIXER | CALLBACK_WINDOW);
  mixerGetDevCaps((UINT)m_hMixer, &m_mxcaps, sizeof(MIXERCAPS));
  mxl.cbStruct = sizeof(MIXERLINE);
  mxl.dwComponentType =MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;

  mixerGetLineInfo((HMIXEROBJ)m_hMixer,&mxl,
   MIXER_OBJECTF_HMIXER|MIXER_GETLINEINFOF_COMPONENTTYPE);


  mxlc.cbStruct = sizeof(mxlc); // 微软用此办法判断版本
  mxlc.dwLineID = mxl.dwLineID; // 上面取得的声卡音频输出线路标识
  // 控制类型为控制音量
  mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
  mxlc.cControls = 1;          // 使用 MIXERCONTROL 结构体变量个数
  mxlc.pamxctrl = &mxc;        // MIXERCONTROL 结构体变量指针
  mxlc.cbmxctrl = sizeof(mxc); // MIXERCONTROL 结构体变量字节大小

  // 取得控制器信息
  mixerGetLineControls((HMIXEROBJ)m_hMixer,
   &mxlc,
   MIXER_GETLINECONTROLSF_ONEBYTYPE);

  MIXERCONTROLDETAILS_UNSIGNED mxcdVolume;
  MIXERCONTROLDETAILS mxcd;
  mxcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
  mxcd.dwControlID = mxc.dwControlID;
  mxcd.cChannels = 1;
  mxcd.cMultipleItems = 0;
  mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
  mxcd.paDetails = &mxcdVolume;

  mixerGetControlDetails((HMIXEROBJ)m_hMixer,&mxcd,
   MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE);
  HANDLE hSpeakGroup=SsnBeginSpeakGroup();
  mxcdVolume.dwValue = mxcdVolume.dwValue - 655;
  if (mxcdVolume.dwValue > 1)
  {
   INT nVolume = INT( mxcdVolume.dwValue/655.35);
   CString ToSpeak = _T("系统音量减小到");
   CString Volume;
   Volume.Format(_T("%d"), nVolume);
   ToSpeak = ToSpeak + Volume;
   SSN_SPEAK_OPTION SpeakOption;
   if (hSpeakGroup!=INVALID_HANDLE_VALUE)
   {
    SsnAddSpeakString(hSpeakGroup,ToSpeak,&SpeakOption);
   }
   mixerSetControlDetails((HMIXEROBJ)m_hMixer,&mxcd,
    MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE);
  }
  else 
  {
   INT nVolume = 0;
   CString ToSpeak = _T("系统音量减小到");
   CString Volume;
   Volume.Format(_T("%d"), nVolume);
   ToSpeak = ToSpeak + Volume;
   SSN_SPEAK_OPTION SpeakOption;
   if (hSpeakGroup!=INVALID_HANDLE_VALUE)
   {
    SsnAddSpeakString(hSpeakGroup,ToSpeak,&SpeakOption);
   }
   mxcdVolume.dwValue = 1;
   mixerSetControlDetails((HMIXEROBJ)m_hMixer,&mxcd,
    MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE);
  }
  SsnEndSpeakGroup(hSpeakGroup);  
  return TRUE;
 }

 //Windows 音量调节
 IMMDevice *defaultDevice = NULL;

 hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
 if (FAILED(hr))
 {
  return FALSE;
 }

 deviceEnumerator->Release();
 deviceEnumerator = NULL;

 IAudioEndpointVolume *endpointVolume;

 hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, reinterpret_cast<void **>(&endpointVolume));
 if (FAILED(hr))
 {
  return FALSE;
 }

 hr = endpointVolume->VolumeStepDown(NULL);
 float currentVolume;
 hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
 INT nVolume = INT(currentVolume * 100);
 HANDLE hSpeakGroup=SsnBeginSpeakGroup();
 CString ToSpeak = _T("系统音量减小到");
 CString Volume;
 Volume.Format(_T("%d"), nVolume);
 ToSpeak = ToSpeak + Volume;
 SSN_SPEAK_OPTION SpeakOption;
 if (hSpeakGroup!=INVALID_HANDLE_VALUE)
 {
  SsnAddSpeakString(hSpeakGroup,ToSpeak,&SpeakOption);
 }
 if (FAILED(hr))
 {
  return FALSE;
 }
 if (hr == S_FALSE)
 {
  return FALSE;
 }
 SsnEndSpeakGroup(hSpeakGroup);  
 return TRUE;
}


http://blog.csdn.net/xlm289348/article/details/8875251

用VC控制系统音量

http://hi.baidu.com/whmtorrent/item/b4c711050e38a334a2332a691、头文件(Mixer.h)// Mixer.h: interface for ...
  • Augusdi
  • Augusdi
  • 2013年04月04日 23:31
  • 3375

VC++控制音量

《Visual C++开发技术大全》第26章声音与动画编程,本章讲述在VC 6.0环境下,如何使用Win32、GDI、GDI+、DirectShow等开发接口实现声音与动画的处理。本小节为大家介绍控制...
  • Augusdi
  • Augusdi
  • 2013年04月04日 23:20
  • 6410

vc音量控制程序,实现了系统音量调节

  • 2010年02月26日 16:49
  • 13.77MB
  • 下载

VC++音频技术之调节音量及设置静音

本文主要介绍如何使用混音器Mixer API函数实现系统音量调节,以及设置静音。 1.混音器的作用及结构 1.1混音器的作用   声卡(音频卡)是计算机进行声音处理的适配器,具有三个基本功能: ...
  • yu_gi_oh
  • yu_gi_oh
  • 2016年06月30日 23:44
  • 2744

vc音量控制类(封装Mixer API)

  • 2010年06月25日 14:12
  • 15KB
  • 下载

用VC控制系统音量

1、头文件(Mixer.h) // Mixer.h: interface for the CMixer class. // ///////////////////////////////////...
  • ccx_john
  • ccx_john
  • 2013年11月05日 16:49
  • 723

C代码实现控制Win 7系统音量

之前的代码基本上是控制Windows XP的代码。可是XP的音量控制代码在Win7上不能生效。 查看了MSDN的相关内容,Win7的代码控制只能通过COM的方式来实现。 如下为详细代码: 本代码...
  • tody_guo
  • tody_guo
  • 2014年04月12日 15:41
  • 3594

vc++ 调节系统音量

  • 2014年07月03日 11:18
  • 26KB
  • 下载

一个利用windows api控制放音音量和录音麦克风音量实现类(c++代码)

1、头文件(Mixer.h)// Mixer.h: interface for the CMixer class. // ///////////////////////////////////...
  • CometNet
  • CometNet
  • 2011年06月11日 08:57
  • 9763

VC实现控制系统音量大小程序(编译通过)

  • 2009年05月19日 09:52
  • 112KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:调节系统音量VC++(适合win和xp系统) 打开静音 增减和减小系统音量
举报原因:
原因补充:

(最多只允许输入30个字)