" property="og:description"/> "/>

Core Audio API控制windows音量

开发环境

-VS 2019
-windows 10 企业版 2016 长期服务版
-其他环境没有测试过

示例代码:

test.h

#pragma once
#include "SystemVolume.h"
#include <iostream>

#ifndef SRC_DEMO_H  
#define SRC_DEMO_H  
extern "C"
{
	
}
#endif

test.cpp

#include "test.h"

using namespace std;

using namespace SystemConf;

int main() {
	SystemVolume systemVolume;
	try {

		cout << "初始化 CoreAudioAPI " << endl;
		systemVolume.init();

		int volume = systemVolume.getVolume();
		cout << "获取当前音量为:" << volume << endl;

		cout << "设置音量为:80" << endl;
		systemVolume.setVolume(80);

		volume = systemVolume.getVolume();
		cout << "获取当前音量为:" << volume << endl;
	
		cout << "关闭服务" << endl;
		systemVolume.close();
	}catch (string e) {
		cout << e << endl;
	}catch (...) {
		cout << "出现异常,关闭服务 释放资源" << endl;
		systemVolume.close();
	}
	return 0;
}

SystemVolume.h

#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

#include  "SystemVolume.h"

namespace SystemConf {

	void SystemVolume::init() {
		hr = CoInitialize(0);
		hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pDeviceEnumerator);
		if (FAILED(hr)) throw "InitException:pDeviceEnumerator is NULL;";
		hr = pDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice);
		if (FAILED(hr)) throw "InitException:pDevice is NULL";
		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";
	}

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

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

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

	void SystemVolume::Mute() {
		hr = pAudioEndpointVolume->SetMute(TRUE, NULL);
		if (FAILED(hr)) throw "Mute() throw Exception";
	}

	void SystemVolume::UnMute() {
		hr = pAudioEndpointVolume->SetMute(FALSE, NULL);
		if (FAILED(hr)) throw "UnMute() throw Exception";
	}
}