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系统Win10 c++音量调节 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1733684371h1611682.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论