Windows下获取扬声器的一些信息

一、获取扬声器是否为静音和音量 

#include "audio_utils.h"

#include <Propkeydef.h>
#include <Functiondiscoverykeys_devpkey.h>
#include <Endpointvolume.h>
#include <audioclient.h>
#include <devicetopology.h>

#include <mmdeviceapi.h>
#include <stdio.h>
#include <tchar.h>
#include <wrl/client.h>

#include <iostream>
#include <memory>

// 获取默认设备的音量
float GetDefaultAudioDeviceVolume() {
	CoInitializeEx(NULL, COINIT_MULTITHREADED);
	// 创建com对象,创建IMMDeviceEnumerator对象
	Microsoft::WRL::ComPtr<IMMDeviceEnumerator> enumerator = nullptr;
	Microsoft::WRL::ComPtr<IMMDevice> device = nullptr;
	Microsoft::WRL::ComPtr<IAudioEndpointVolume> endpoint_volume = nullptr;
	std::shared_ptr<void> raii_ptr(nullptr, [&](void*) {
		endpoint_volume = nullptr;
		device = nullptr;
		enumerator = nullptr;
		CoUninitialize();
	});

	HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL,
		__uuidof(IMMDeviceEnumerator), (void**)enumerator.GetAddressOf());
	if (hr != S_OK) {
		std::cout << "CoCreateInstance error" << std::endl;
		return -1;
	}
	// 获取指定EDataFlow和ERole方向上的设备
	// eRender:渲染设备的数据流方向,扬声器,对应eCapture:麦克风
	// eConsole:游戏,系统通知声音和命令
	hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, device.GetAddressOf());
	if (hr != S_OK) {
		std::cout << "GetDefaultAudioEndpoint error" << std::endl;
		return -1;
	}
	// 创建IAudioEndpointVolume对象
	hr = device->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER,
		NULL, (void**)endpoint_volume.GetAddressOf());
	if (hr != S_OK) {
		std::cout << "Activate error" << std::endl;
		return -1;
	}
	float volume_sacle;
	endpoint_volume->GetMasterVolumeLevelScalar(&volume_sacle);
	return volume_sacle;
}
// 设置默认设备的音量
void SetDefaultAudioDeviceVolume(float volume) {
	CoInitializeEx(NULL, COINIT_MULTITHREADED);
	Microsoft::WRL::ComPtr<IMMDeviceEnumerator> enumerator = nullptr;
	Microsoft::WRL::ComPtr<IMMDevice> device = nullptr;
	Microsoft::WRL::ComPtr<IAudioEndpointVolume> endpoint_volume = nullptr;
	std::shared_ptr<void> raii_ptr(nullptr, [&](void*) {
		endpoint_volume = nullptr;
		device = nullptr;
		enumerator = nullptr;
		CoUninitialize();
	});
	HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL,
		__uuidof(IMMDeviceEnumerator), (void**)enumerator.GetAddressOf());
	if (hr != S_OK) {
		std::cout << "CoCreateInstance error" << std::endl;
		return;
	}

	hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, device.GetAddressOf());
	if (hr != S_OK) {
		std::cout << "GetDefaultAudioEndpoint error" << std::endl;
		return;
	}

	hr = device->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER,
		NULL, (void**)endpoint_volume.GetAddressOf());
	if (hr != S_OK) {
		std::cout << "Activate error" << std::endl;
		return;
	}
	endpoint_volume->SetMasterVolumeLevelScalar(volume, nullptr);
}
// 默认设备是不是静音
bool IsDefaultAudioDeviceMuted() {
	CoInitializeEx(NULL, COINIT_MULTITHREADED);
	Microsoft::WRL::ComPtr<IMMDeviceEnumerator> enumerator = nullptr;
	Microsoft::WRL::ComPtr<IMMDevice> device = nullptr;
	Microsoft::WRL::ComPtr<IAudioEndpointVolume> endpoint_volume = nullptr;
	std::shared_ptr<void> raii_ptr(nullptr, [&](void*) {
		endpoint_volume = nullptr;
		device = nullptr;
		enumerator = nullptr;
		CoUninitialize();
	});
	HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL,
		__uuidof(IMMDeviceEnumerator), (void**)enumerator.GetAddressOf());
	if (hr != S_OK) {
		std::cout << "CoCreateInstance error" << std::endl;
		return false;
	}

	hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, device.GetAddressOf());
	if (hr != S_OK) {
		std::cout << "GetDefaultAudioEndpoint error" << std::endl;
		return false;
	}

	hr = device->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER,
		NULL, (void**)endpoint_volume.GetAddressOf());
	if (hr != S_OK) {
		std::cout << "Activate error" << std::endl;
		return false;
	}

	BOOL mute = false;
	endpoint_volume->GetMute(&mute);
	return mute;
}
// 设置默认设备是否为静音
void SetDefaultAudioDeviceMuted(bool mute) {
	BOOL bmute = mute;
	CoInitializeEx(NULL, COINIT_MULTITHREADED);
	Microsoft::WRL::ComPtr<IMMDeviceEnumerator> enumerator = nullptr;
	Microsoft::WRL::ComPtr<IMMDevice> device = nullptr;
	Microsoft::WRL::ComPtr<IAudioEndpointVolume> endpoint_volume = nullptr;
	std::shared_ptr<void> raii_ptr(nullptr, [&](void*) {
		endpoint_volume = nullptr;
		device = nullptr;
		enumerator = nullptr;
		CoUninitialize();
	});
	HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL,
		__uuidof(IMMDeviceEnumerator), (void**)enumerator.GetAddressOf());
	if (hr != S_OK) {
		std::cout << "CoCreateInstance error" << std::endl;
		return;
	}

	hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, device.GetAddressOf());
	if (hr != S_OK) {
		std::cout << "GetDefaultAudioEndpoint error" << std::endl;
		return;
	}

	hr = device->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER,
		NULL, (void**)endpoint_volume.GetAddressOf());
	if (hr != S_OK) {
		std::cout << "Activate error" << std::endl;
		return;
	}
	endpoint_volume->SetMute(bmute, nullptr);
}

二、获取扬声器的名字和id

void RequireAudioInfo() {
	Microsoft::WRL::ComPtr<IMMDeviceEnumerator> device_enumerator = nullptr;
	Microsoft::WRL::ComPtr<IMMDevice> device = nullptr;
	Microsoft::WRL::ComPtr<IMMDeviceCollection> collection = nullptr;
	LPWSTR current_device_id = NULL;

	HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);

	std::shared_ptr<void> raii_ptr(nullptr, [&](void*) {
		collection = nullptr;
		device = nullptr;
		device_enumerator = nullptr;
		CoUninitialize();
	});
	hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL,
		__uuidof(IMMDeviceEnumerator), (void**)device_enumerator.GetAddressOf());
	hr = device_enumerator->GetDefaultAudioEndpoint(eRender, eConsole, device.GetAddressOf());
	hr = device_enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, collection.GetAddressOf());
	UINT count;
	hr = collection->GetCount(&count);

	device->GetId(&current_device_id);
	std::wcout << "current_device_id:" << current_device_id << std::endl;
	CoTaskMemFree(current_device_id);

	// 打印音频设备个数
	std::cout << "count:" << count << std::endl;

	for (int i = 0; i < count; ++i) {
		IMMDevice* pEndpointDevice = NULL;
		IDeviceTopology* deviceTopology = NULL;
		IConnector* connector = NULL;

		LPWSTR device_name = NULL;
		LPWSTR device_id = NULL;

		hr = collection->Item(i, &pEndpointDevice);
		hr = pEndpointDevice->Activate(__uuidof(IDeviceTopology), CLSCTX_INPROC_SERVER, NULL,
			(LPVOID*)&deviceTopology);
		hr = deviceTopology->GetConnector(0, &connector);

		hr = connector->GetConnectorIdConnectedTo(&device_name);
		std::wcout << "device name:" << device_name << std::endl;
		
		hr = pEndpointDevice->GetId(&device_id);
		std::wcout << "device id:" << device_id << std::endl;

		CoTaskMemFree(device_name);
		CoTaskMemFree(device_id);
	}
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值