admin 管理员组

文章数量: 887007

1.简述

Qt原生是不支持系统音量调节的,需要用到windows的API,效果如下

2.代码

测试代码

    QSlider *pSliderValume = new QSlider(this);
    pSliderValume->setValue(sysVolume());  //CWindows.h
    connect(pSliderValume,&QSlider::sigValueChanged,[=](int nVal){
        setSysVolume(nVal); //CWindows.h
    });
    /*监听系统音量变化*/
    QTimer *pTimerListenVolume = new QTimer(this);
    pTimerListenVolume->start(100);
    connect(pTimerListenVolume,&QTimer::timeout,[=]{
        int nSysValume = sysVolume();
        if(pSliderValume->value() != nSysValume)
            pSliderValume->setValue(nSysValume);
    });

接口头文件

#ifdef Q_OS_WIN
    bool setSysVolume(int level);
    int sysVolume();
#endif //Q_OS_WIN

接口源文件

#ifdef Q_OS_WIN
#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <audioclient.h>

//参数:
//	-2 恢复静音
//	-1 静音
//	0~100:音量比例
bool setSysVolume(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 sysVolume()
{
    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";

        float fVolume;

        hr = pAudioEndpointVolume->GetMasterVolumeLevelScalar(&fVolume);

        if(FAILED(hr)) throw "SetMasterVolumeLevelScalar";

        pAudioClient->Release();
        pAudioEndpointVolume->Release();
        pDevice->Release();
        pDeviceEnumerator->Release();

        int  intVolume = fVolume*100+1;
        if(fVolume>100)
        {
            fVolume =100;
        }
        return intVolume;

    }

    catch(...){
        if(pAudioClient) pAudioClient->Release();
        if(pAudioEndpointVolume) pAudioEndpointVolume->Release();
        if(pDevice) pDevice->Release();
        if(pDeviceEnumerator) pDeviceEnumerator->Release();
        throw;
    }
}
#endif

本文标签: 音量调节 系统 QT Windows