tianyu的专栏

-- 宁静致远

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

#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

阅读更多
个人分类: DirectX
上一篇Windows 7、Windows Server 2008等系统音频系统底层API
下一篇Win7系统下的立体声混音录音的声卡的设置方法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭