51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Core Audio API控制windows音量

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 &amp;lt;&amp;lt; &quot;初始化 CoreAudioAPI &quot; &amp;lt;&amp;lt; endl;
	systemVolume.init();

	int volume = systemVolume.getVolume();
	cout &amp;lt;&amp;lt; &quot;获取当前音量为:&quot; &amp;lt;&amp;lt; volume &amp;lt;&amp;lt; endl;

	cout &amp;lt;&amp;lt; &quot;设置音量为:80&quot; &amp;lt;&amp;lt; endl;
	systemVolume.setVolume(80);

	volume = systemVolume.getVolume();
	cout &amp;lt;&amp;lt; &quot;获取当前音量为:&quot; &amp;lt;&amp;lt; volume &amp;lt;&amp;lt; endl;

	cout &amp;lt;&amp;lt; &quot;关闭服务&quot; &amp;lt;&amp;lt; endl;
	systemVolume.close();
}catch (string e) {
	cout &amp;lt;&amp;lt; e &amp;lt;&amp;lt; endl;
}catch (...) {
	cout &amp;lt;&amp;lt; &quot;出现异常,关闭服务 释放资源&quot; &amp;lt;&amp;lt; endl;
	systemVolume.close();
}
return 0;

}


SystemVolume.h {#systemvolumeh}

#pragma once

#ifndef SystemVolume_h

#include &lt;windows.h&gt; #include &lt;mmdeviceapi.h&gt; #include &lt;endpointvolume.h&gt; #include &lt;audioclient.h&gt;

#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;amp;pDeviceEnumerator);
	if (FAILED(hr)) throw &quot;InitException:pDeviceEnumerator is NULL;&quot;;
	hr = pDeviceEnumerator-&amp;gt;GetDefaultAudioEndpoint(eRender, eMultimedia, &amp;amp;pDevice);
	if (FAILED(hr)) throw &quot;InitException:pDevice is NULL&quot;;
	hr = pDevice-&amp;gt;Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&amp;amp;pAudioEndpointVolume);
	if (FAILED(hr)) throw &quot;pDevice-&amp;gt;Active&quot;;
	hr = pDevice-&amp;gt;Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&amp;amp;pAudioClient);
	if (FAILED(hr)) throw &quot;pDevice-&amp;gt;Active&quot;;
}

void SystemVolume::close() {
	if (pAudioClient) pAudioClient-&amp;gt;Release();
	if (pAudioEndpointVolume) pAudioEndpointVolume-&amp;gt;Release();
	if (pDevice) pDevice-&amp;gt;Release();
	if (pDeviceEnumerator) pDeviceEnumerator-&amp;gt;Release();
	CoUninitialize();
}

void SystemVolume::setVolume(int volume) {
	float fVolume = volume / 100.0f;
	hr = pAudioEndpointVolume-&amp;gt;SetMasterVolumeLevelScalar(fVolume, &amp;amp;GUID_NULL);
	if (FAILED(hr)) throw &quot;SetMasterVolumeLevelScalar&quot;;
}

int SystemVolume::getVolume() {
	float volume;
	hr = pAudioEndpointVolume-&amp;gt;GetMasterVolumeLevelScalar(&amp;amp;volume);
	if (FAILED(hr)) throw &quot;getVolume() throw Exception&quot;;
	return (int)round(volume*100.0);
}

void SystemVolume::Mute() {
	hr = pAudioEndpointVolume-&amp;gt;SetMute(TRUE, NULL);
	if (FAILED(hr)) throw &quot;Mute() throw Exception&quot;;
}

void SystemVolume::UnMute() {
	hr = pAudioEndpointVolume-&amp;gt;SetMute(FALSE, NULL);
	if (FAILED(hr)) throw &quot;UnMute() throw Exception&quot;;
}

}


赞(1)
未经允许不得转载:工具盒子 » Core Audio API控制windows音量