Core Audio API控制windows音量 {#core-audio-api控制windows音量}
开发环境 {#开发环境}
-VS 2019
-windows 10 企业版 2016 长期服务版
-其他环境没有测试过
示例代码: {#示例代码}
test.h {#testh}
#pragma once
#include "SystemVolume.h"
#include <iostream>
#ifndef SRC_DEMO_H
#define SRC_DEMO_H
extern "C"
{
} #endif
test.cpp {#testcpp}
#include "test.h"
using namespace std;
using namespace SystemConf;
int main() {
SystemVolume systemVolume;
try {
cout &lt;&lt; "初始化 CoreAudioAPI " &lt;&lt; endl;
systemVolume.init();
int volume = systemVolume.getVolume();
cout &lt;&lt; "获取当前音量为:" &lt;&lt; volume &lt;&lt; endl;
cout &lt;&lt; "设置音量为:80" &lt;&lt; endl;
systemVolume.setVolume(80);
volume = systemVolume.getVolume();
cout &lt;&lt; "获取当前音量为:" &lt;&lt; volume &lt;&lt; endl;
cout &lt;&lt; "关闭服务" &lt;&lt; endl;
systemVolume.close();
}catch (string e) {
cout &lt;&lt; e &lt;&lt; endl;
}catch (...) {
cout &lt;&lt; "出现异常,关闭服务 释放资源" &lt;&lt; endl;
systemVolume.close();
}
return 0;
}
SystemVolume.h {#systemvolumeh}
#pragma once
#ifndef SystemVolume_h
#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <audioclient.h>
#define SystemVolume_h
namespace SystemConf {
class SystemVolume {
private:
HRESULT hr;
IMMDeviceEnumerator* pDeviceEnumerator = 0;
IMMDevice* pDevice = 0;
IAudioEndpointVolume* pAudioEndpointVolume = 0;
IAudioClient* pAudioClient = 0;
public:
/**初始化服务*/
void init();
/**关闭服务 释放资源*/
void close();
/**设置音量*/
void setVolume(int volume);
/**获取系统音量*/
int getVolume();
/**静音*/
void Mute();
/**解除静音*/
void UnMute();
};
}
#endif
SystemVolume.cpp {#systemvolumecpp}
#include "SystemVolume.h"
namespace SystemConf {
void SystemVolume::init() {
hr = CoInitialize(0);
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&amp;pDeviceEnumerator);
if (FAILED(hr)) throw "InitException:pDeviceEnumerator is NULL;";
hr = pDeviceEnumerator-&gt;GetDefaultAudioEndpoint(eRender, eMultimedia, &amp;pDevice);
if (FAILED(hr)) throw "InitException:pDevice is NULL";
hr = pDevice-&gt;Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&amp;pAudioEndpointVolume);
if (FAILED(hr)) throw "pDevice-&gt;Active";
hr = pDevice-&gt;Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&amp;pAudioClient);
if (FAILED(hr)) throw "pDevice-&gt;Active";
}
void SystemVolume::close() {
if (pAudioClient) pAudioClient-&gt;Release();
if (pAudioEndpointVolume) pAudioEndpointVolume-&gt;Release();
if (pDevice) pDevice-&gt;Release();
if (pDeviceEnumerator) pDeviceEnumerator-&gt;Release();
CoUninitialize();
}
void SystemVolume::setVolume(int volume) {
float fVolume = volume / 100.0f;
hr = pAudioEndpointVolume-&gt;SetMasterVolumeLevelScalar(fVolume, &amp;GUID_NULL);
if (FAILED(hr)) throw "SetMasterVolumeLevelScalar";
}
int SystemVolume::getVolume() {
float volume;
hr = pAudioEndpointVolume-&gt;GetMasterVolumeLevelScalar(&amp;volume);
if (FAILED(hr)) throw "getVolume() throw Exception";
return (int)round(volume*100.0);
}
void SystemVolume::Mute() {
hr = pAudioEndpointVolume-&gt;SetMute(TRUE, NULL);
if (FAILED(hr)) throw "Mute() throw Exception";
}
void SystemVolume::UnMute() {
hr = pAudioEndpointVolume-&gt;SetMute(FALSE, NULL);
if (FAILED(hr)) throw "UnMute() throw Exception";
}
}