windows下修改系统音量

#include  
#include  
#include 
#include 


//参数:
//	-2 恢复静音
//	-1 静音
//	0~100:音量比例
bool SetVolumeLevel(int level)
{
  HRESULT hr;
  IMMDeviceEnumerator* pDeviceEnumerator=0;
  IMMDevice* pDevice=0;
  IAudioEndpointVolume* pAudioEndpointVolume=0;
  IAudioClient* pAudioClient=0;

  try{
    hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),NULL,CLSCTX_ALL,__uuidof(IMMDeviceEnumerator),(void**)&pDeviceEnumerator);
    if(FAILED(hr)) throw "CoCreateInstance";
    hr = pDeviceEnumerator->GetDefaultAudioEndpoint(eRender,eMultimedia,&pDevice);
    if(FAILED(hr)) throw "GetDefaultAudioEndpoint";
    hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),CLSCTX_ALL,NULL,(void**)&pAudioEndpointVolume);
    if(FAILED(hr)) throw "pDevice->Active";
    hr = pDevice->Activate(__uuidof(IAudioClient),CLSCTX_ALL,NULL,(void**)&pAudioClient);
    if(FAILED(hr)) throw "pDevice->Active";

    if(level==-2){
      hr = pAudioEndpointVolume->SetMute(FALSE,NULL);
      if(FAILED(hr)) throw "SetMute";
    }else if(level==-1){
      hr = pAudioEndpointVolume->SetMute(TRUE,NULL);
      if(FAILED(hr)) throw "SetMute";
    }else{
      if(level<0 || level>100){
        hr = E_INVALIDARG;
        throw "Invalid Arg";
      }

      float fVolume;
      fVolume = level/100.0f;
      hr = pAudioEndpointVolume->SetMasterVolumeLevelScalar(fVolume,&GUID_NULL);
      if(FAILED(hr)) throw "SetMasterVolumeLevelScalar";

      pAudioClient->Release();
      pAudioEndpointVolume->Release();
      pDevice->Release();
      pDeviceEnumerator->Release();
      return true;
    }
  }
  catch(...){
    if(pAudioClient) pAudioClient->Release();
    if(pAudioEndpointVolume) pAudioEndpointVolume->Release();
    if(pDevice) pDevice->Release();
    if(pDeviceEnumerator) pDeviceEnumerator->Release();
    throw;
  }
  return false;
}



int main()
{
  CoInitialize(0);
  try{
    //3秒后静音
    Sleep(3000);
    SetVolumeLevel(-1);
    //3秒后恢复静音
    Sleep(3000);
    SetVolumeLevel(-2);
    //调节音量
    Sleep(3000);
    SetVolumeLevel(10);
    Sleep(3000);
    SetVolumeLevel(30);
    Sleep(3000);
    SetVolumeLevel(20);
  }
  catch(...){
    //错误处理...
  }
  CoUninitialize();
  return 0;
}

你可能感兴趣的:(编程技巧)