当前位置: 首页 > news >正文

vc++在Windows中查找扬声器输出的当前音量级别

我正在尝试编写一种音频可视化工具,我需要获取当前发送到扬声器的声音音量。我需要声音的实际音量,而不仅仅是设备的主音量。我一直在尝试使用this来实现它,但我不确定如何实现它。调用了IAudioCaptureClient::GetBuffer()方法之后,我不知道如何使用生成的数据包来查找所需的内容。音频为2声道,每个采样32位。任何帮助都将不胜感激

#include <Windows.h>
#include <stdio.h>
#include <mmeapi.h>
#include <mmdeviceapi.h> 
#include <endpointvolume.h>
#include <audioclient.h>#pragma comment(lib,"Winmm.lib")bool GetVolumeLevel()
{HRESULT hr;IMMDeviceEnumerator* pDeviceEnumerator = 0;IMMDevice* pDevice = 0;IAudioEndpointVolume* pAudioEndpointVolume = 0;try {hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pDeviceEnumerator);if (FAILED(hr)) throw "CoCreateInstance";hr = pDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice);if (FAILED(hr)) throw "GetDefaultAudioEndpoint";hr = pDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&pAudioEndpointVolume);if (FAILED(hr)) throw "pDevice->Active";float fVolume;hr = pAudioEndpointVolume->GetMasterVolumeLevelScalar(&fVolume);printf("%lf", fVolume);if (FAILED(hr)) throw "SetMasterVolumeLevelScalar";pAudioEndpointVolume->Release();pDevice->Release();pDeviceEnumerator->Release();return true;}catch (...) {if (pAudioEndpointVolume) pAudioEndpointVolume->Release();if (pDevice) pDevice->Release();if (pDeviceEnumerator) pDeviceEnumerator->Release();throw;}return false;
}int main()
{CoInitialize(0);try {GetVolumeLevel();}catch (...) {//err handle..}CoUninitialize();getchar();return 0;
}

http://www.xdnf.cn/news/632233.html

相关文章:

  • DeepSeek+白果AI论文:开启答辩PPT生成的「智能双引擎」时代
  • 国际前沿知识系列二:基于不同类型头部碰撞中的运动学特征预测能力统计分析
  • 005 深度优先搜索(DFS)算法详解:图解+代码+经典例题
  • C++编译/链接模型
  • Fuzz 模糊测试篇JS 算法口令隐藏参数盲 Payload未知文件目录
  • 基于 ColBERT 框架的后交互 (late interaction) 模型速递:Reason-ModernColBERT
  • 广东省省考备考(第十九天5.24)—言语(第七节课)
  • Qwen2.5 VL 多模态融合阶段(3)
  • python炸鱼船
  • java基础(继承)
  • 【免费使用】剪Y专业版 8.1/CapCut 视频编辑处理,素材和滤镜
  • Spyglass:跨时钟域同步(长延迟信号)
  • 异步处理与事件驱动中的模型调用链设计
  • 5.24 打卡
  • 【电池】极端环境对锂离子电池的影响-【2.5万字解析】
  • 数值分析(电子和通信类)
  • 什么是电离层闪烁
  • WPS 64位与EndNote21.5工作流
  • 【大模型】TableLLM论文总结
  • 力扣刷题(第三十六天)
  • C++ class和struct的区别
  • AI专题:如何把DeepSeek变成你的AI个人助手
  • 虚拟环境中的PyQt5 Pycharm设置参考
  • 多态的总结
  • http协议和session会话
  • 变革性的聚变路线:基于FRC构型的可控核聚变
  • leetcode 862. 和至少为 K 的最短子数组
  • 2025深圳国际无人机展深度解析:看点、厂商与创新亮点
  • 何谓第二大脑?读书笔记
  • 进一步学习线程相关知识