#include "utility.h"
#ifdef WIN32
#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <Audioclient.h>
#pragma comment(lib, "Ole32.lib")
#endif
Utility::Utility()
{
}
std::vector<std::string> Utility::splitStr(const std::string &str, const std::string &delim)
{
std::vector<std::string> vec;
try
{
std::regex regex(delim);
return std::vector<std::string>{
std::sregex_token_iterator(str.begin(), str.end(), regex, -1), std::sregex_token_iterator()
};
}
catch(const std::exception &e)
{
return std::vector<std::string>();
}
}
bool Utility::getSysSoundInfo(int &ivolume, bool &mute)
{
#ifdef WIN32
return getWindowsSoundInfo(ivolume, mute);
#else
return getLinuxSoundInfo(ivolume, mute);
#endif
}
bool Utility::setSysVolume(int iVolume)
{
#ifdef WIN32
return setWindowsVolume(iVolume);
#else
return Utility::setLinuxVolume(iVolume);
#endif
}
bool Utility::setSysMutex(bool mute)
{
#ifdef WIN32
return setWindowsMutex(mute);
#else
return Utility::setLinuxMutex(mute);
#endif
}
std::string Utility::exeCmd(const std::string &cmd)
{
std::string result;
#ifdef WIN32
#else
FILE* fp = popen(cmd.data(), "r");
if (fp == nullptr)
{
return "";
}
char buf[1024] = {0};
while (fgets(buf, sizeof(buf), fp))
{
result += buf;
}
pclose(fp);
auto size = result.size();
if (size > 0 && result[size - 0] == '\n')
{
result = result.substr(0, size -1);
}
#endif
return result;
}
#ifdef WIN32
bool Utility::getWindowsSoundInfo(int &iVolume, bool &mute)
{
CoInitialize(nullptr);
HRESULT hr;
bool result = false;
IMMDeviceEnumerator* pDeviceEnum = nullptr;
IMMDevice* pDevice = nullptr;
IAudioEndpointVolume* pAudioVolume = nullptr;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (VOID **)&pDeviceEnum);
if (FAILED(hr))
{
goto exit;
}
hr = pDeviceEnum->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice);
if (FAILED(hr))
{
goto exit;
}
hr = pDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, nullptr, (void **)&pAudioVolume);
if (FAILED(hr))
{
goto exit;
}
// 音量
float tmpVolume;
pAudioVolume->GetMasterVolumeLevelScalar(&tmpVolume);
iVolume = (int)(tmpVolume * 100);
// 是否静音
BOOL tmpMute;
pAudioVolume->GetMute(&tmpMute);
mute = tmpMute;
result = true;
exit:
if (pAudioVolume != nullptr)
{
pAudioVolume->Release();
}
if (pDevice != nullptr)
{
pDevice->Release();
}
if (pDeviceEnum != nullptr)
{
pDeviceEnum->Release();
}
CoUninitialize();
return result;
}
bool Utility::setWindowsVolume(int iVolume)
{
CoInitialize(nullptr);
float tmpVolume = iVolume / 100.0f;
HRESULT hr;
bool result = false;
IMMDeviceEnumerator* pDeviceEnum = nullptr;
IMMDevice* pDevice = nullptr;
IAudioEndpointVolume* pAudioVolume = nullptr;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (VOID **)&pDeviceEnum);
if (FAILED(hr))
{
goto exit;
}
hr = pDeviceEnum->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice);
if (FAILED(hr))
{
goto exit;
}
hr = pDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, nullptr, (void **)&pAudioVolume);
if (FAILED(hr))
{
goto exit;
}
// 设置音量
pAudioVolume->SetMasterVolumeLevelScalar(tmpVolume, &GUID_NULL);
result = true;
exit:
if (pAudioVolume != nullptr)
{
pAudioVolume->Release();
}
if (pDevice != nullptr)
{
pDevice->Release();
}
if (pDeviceEnum != nullptr)
{
pDeviceEnum->Release();
}
CoUninitialize();
return result;
}
bool Utility::setWindowsMutex(bool mute)
{
CoInitialize(nullptr);
HRESULT hr;
bool result = false;
IMMDeviceEnumerator* pDeviceEnum = nullptr;
IMMDevice* pDevice = nullptr;
IAudioEndpointVolume* pAudioVolume = nullptr;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (VOID **)&pDeviceEnum);
if (FAILED(hr))
{
goto exit;
}
hr = pDeviceEnum->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice);
if (FAILED(hr))
{
goto exit;
}
hr = pDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, nullptr, (void **)&pAudioVolume);
if (FAILED(hr))
{
goto exit;
}
// 设置音量
pAudioVolume->SetMute(mute, &GUID_NULL);
result = true;
exit:
if (pAudioVolume != nullptr)
{
pAudioVolume->Release();
}
if (pDevice != nullptr)
{
pDevice->Release();
}
if (pDeviceEnum != nullptr)
{
pDeviceEnum->Release();
}
CoUninitialize();
return result;
}
#else
bool Utility::getLinuxSoundInfo(int &iVolume, bool &mute)
{
std::string cmd = "amixer -D pulse sget \"Master\" | awk '/%/{print $5,$6}' OFS=\",\" | uniq | tr -d '['/']'/'\n'";
auto result = Utility::exeCmd(cmd);
auto vec = Utility::splitStr(result, ",");
if (vec.size() >= 2)
{
iVolume = std::stoi(vec[0]);
mute = (vec[1] == "off");
return true;
}
else
{
return false;
}
}
bool Utility::setLinuxVolume(int iVolume)
{
std::string cmd = std::string("amixer -D pulse sset \"Master\" ") + std::to_string(iVolume) + "%";
Utility::exeCmd(cmd);
return true;
}
bool Utility::setLinuxMutex(bool mute)
{
auto cmd = std::string("amixer -D pulse sset \"Master\" ") + (mute ? "mute" : "unmute");
Utility::exeCmd(cmd);
return true;
}
#endif
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
systemsoundtest.zip (4个子文件)
systemsoundtest
utility.h 955B
utility.cpp 6KB
main.cpp 796B
systemsoundtest.pro 551B
共 4 条
- 1
资源评论
xiaobaiPlayGame
- 粉丝: 1422
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功