c++设置windons默认输出音频设备
主要功能是控制默认输出的音频设备进行切换。
本文主要教导如何使用c++进行控制
代码示例
#include <windows.h>
#include <mmdeviceapi.h>
#include <iostream>
#include <functiondiscoverykeys_devpkey.h>
#include <vector>
#include "PolicyConfig.h"
#include <Propidl.h>
#pragma comment(lib, "ole32.lib")
// 定义设备信息结构
struct DeviceInfo {
wchar_t* name;
wchar_t* id;
};
// Helper function to convert wide string to multi-byte string
std::string WStringToString(const std::wstring& wstr) {
if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}
// 获取设备的ID和名称
DeviceInfo GetDeviceInfo(IMMDevice* device) {
DeviceInfo deviceInfo;
deviceInfo.id = nullptr;
deviceInfo.name = nullptr;
LPWSTR deviceId = nullptr;
device->GetId(&deviceId);
IPropertyStore* pStore = nullptr;
device->OpenPropertyStore(STGM_READ, &pStore);
PROPVARIANT varName;
PropVariantInit(&varName);
pStore->GetValue(PKEY_Device_FriendlyName, &varName);
deviceInfo.name = _wcsdup(varName.pwszVal);
deviceInfo.id = _wcsdup(deviceId); // 复制ID字符串
PropVariantClear(&varName);
pStore->Release();
CoTaskMemFree(deviceId); // 释放获取到的设备ID内存
return deviceInfo;
}
// 获取当前默认音频播放设备的ID和名称
DeviceInfo GetCurrentDefaultAudioDevice() {
IMMDeviceEnumerator* pEnum = nullptr;
CoInitialize(nullptr);
HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnum);
if (FAILED(hr)) return { nullptr, nullptr };
IMMDevice* pDefDevice = nullptr;
hr = pEnum->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDefDevice);
DeviceInfo deviceInfo = { nullptr, nullptr };
if (SUCCEEDED(hr)) {
deviceInfo = GetDeviceInfo(pDefDevice);
pDefDevice->Release();
}
pEnum->Release();
CoUninitialize();
return deviceInfo;
}
// 获取所有音频输出设备
DeviceInfo* GetAudioOutputDevices(int* count) {
std::vector<DeviceInfo> deviceList;
HRESULT hr = CoInitialize(nullptr);
if (FAILED(hr)) return nullptr;
IMMDeviceEnumerator* pEnumerator = nullptr;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (void**)&pEnumerator);
if (FAILED(hr)) {
CoUninitialize();
return nullptr;
}
IMMDeviceCollection* pCollection = nullptr;
hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pCollection);
if (FAILED(hr)) {
pEnumerator->Release();
CoUninitialize();
return nullptr;
}
UINT deviceCount;
pCollection->GetCount(&deviceCount);
*count = deviceCount; // 设置设备数量
DeviceInfo* devices = new DeviceInfo[deviceCount]; // 动态分配数组
for (UINT i = 0; i < deviceCount; ++i) {
IMMDevice* pDevice = nullptr;
hr = pCollection->Item(i, &pDevice);
if (SUCCEEDED(hr)) {
devices[i] = GetDeviceInfo(pDevice);
pDevice->Release();
}
}
pCollection->Release();
pEnumerator->Release();
CoUninitialize();
return devices;
}
// 设置默认音频播放设备
bool SetDefaultAudioPlaybackDevice(LPCWSTR devID) {
// 初始化 COM 库
HRESULT comHr = CoInitialize(nullptr);
if (FAILED(comHr)) {
std::cerr << "COM库初始化失败,错误代码: " << comHr << std::endl;
return false;
}
IPolicyConfigVista* pPolicyConfig;
HRESULT hr = CoCreateInstance(__uuidof(CPolicyConfigVistaClient), NULL, CLSCTX_ALL, __uuidof(IPolicyConfigVista), (LPVOID*)&pPolicyConfig);
if (SUCCEEDED(hr)) {
hr = pPolicyConfig->SetDefaultEndpoint(devID, eConsole);
pPolicyConfig->Release();
if (FAILED(hr)) {
std::cout << WStringToString(L"设置默认设备失败,错误代码: ") << hr << std::endl;
}
return SUCCEEDED(hr);
}
else {
std::cout << WStringToString( L"创建 PolicyConfig 实例失败,错误代码: ") << hr << std::endl;
}
CoUninitialize();
return false;
}
void FreeDeviceArray(DeviceInfo* devices, int count) {
for (int i = 0; i < count; i++) {
free(devices[i].name); // 释放设备名称内存
free(devices[i].id); // 释放设备ID内存
}
free(devices); // 释放设备数组内存
}
int main() {
SetConsoleOutputCP(65001); // Set console output code page to UTF-8
// 获取并显示当前默认设备
DeviceInfo defaultDevice = GetCurrentDefaultAudioDevice();
std::cout << WStringToString(L"当前默认音频设备: ") << WStringToString(defaultDevice.name)
<< " (ID: " << WStringToString(defaultDevice.id) << ")" << std::endl;
// 获取可用设备列表
int deviceCount;
DeviceInfo* devices = GetAudioOutputDevices(&deviceCount);
std::cout << WStringToString(L"可用音频输出设备:") << std::endl;
for (int i = 0; i < deviceCount; ++i) {
std::cout << i + 1 << ". " << WStringToString(devices[i].name) << std::endl;
}
int choice;
std::cout << WStringToString(L"选择一个设备编号作为默认设备: ");
std::cin >> choice;
if (choice > 0 && choice <= deviceCount) {
const auto& selectedDevice = devices[choice - 1];
if (SetDefaultAudioPlaybackDevice(selectedDevice.id)) {
std::cout << WStringToString(L"已将默认设备设置为: ") << WStringToString(selectedDevice.name) << std::endl;
}
else {
std::cout << WStringToString(L"无法设置默认设备。") << std::endl;
}
}
else {
std::cout << WStringToString(L"无效选择。") << std::endl;
}
// 释放设备信息内存
free(defaultDevice.name); // 释放默认设备名称内存
free(defaultDevice.id); // 释放默认设备ID内存
FreeDeviceArray(devices, deviceCount); // 释放设备数组内存
return 0;
}
函数功能
GetCurrentDefaultAudioDevice: 获取当前默认音频播放设备的ID和名称。
GetAudioOutputDevices: 获取所有音频输出设备。
SetDefaultAudioPlaybackDevice: 设置默认音频播放设备。
这里引用了一个.h文件
#include “PolicyConfig.h”
CSDN下载地址