设为首页 收藏本站
查看: 2014|回复: 0

Core Audio APIs (Win Vista , Win7) 获取一个指定设备的音量

[复制链接]

尚未签到

发表于 2015-5-19 12:49:54 | 显示全部楼层 |阅读模式
MSDN 关于 Core Audio APIs 的索引:
  This guide section explains the concepts and features of the core audio APIs of Windows Vista, and describes how to use them in application programming.
  This section contains the following topics.
  Topic
Description
  User-Mode Audio Components
Through the low-level interfaces in the core audio APIs, a client can access the system components that manage and mix audio streams.
  Protected User Mode Audio (PUMA)
Describes the updates to Protected User Mode Audio (PUMA), the user-mode audio engine in the Protected Environment (PE), which provides a safer environment for audio processing and rendering.
  Audio Endpoint Devices
An audio endpoint device is a software abstraction that enables user-friendly interactions with audio devices such as microphones and speakers.
  Audio Sessions
An audio session is a software abstraction that enables a client to manage a collection of related audio streams as a single unit.
  Volume Controls
The system integrates its policy-based volume settings with the user's volume settings in a logical and consistent way.
  Stream Management
The Windows Audio Session API (WASAPI) provides a client with a complete set of methods for creating and managing audio streams.
  Device Topologies
The DeviceTopology API enables a client to discover the audio controls that lie along the various data paths in the audio hardware.
  Using the IKsControl Interface to Access Audio Properties
A specialized audio application might need to use the IKsControl interface to access the properties of an audio adapter.
  Interoperability with Legacy Audio APIs
Key features of the core audio APIs in Windows Vista can be incorporated into existing applications that use DirectSound, DirectShow, and the Windows multimedia waveOutXxx and waveInXxx functions.
  
  用一张图来了解下 Core Audio APIs 各接口之间的关系
DSC0000.jpg
  
  

#include "stdafx.h"
#include "mmdeviceapi.h"
#include "Endpointvolume.h"
//#include "Audioclient.h"
//#include "Audiopolicy.h"

#define SAFE_RELEASE(punk)  \
if ((punk) != NULL)  \
{ (punk)->Release(); (punk) = NULL; }
//得到设备硬件ID (设备管理器可以看到的硬件ID)
bool GetDeviceDsc(IMMDevice *pDevice,wchar_t* DeviceDsc)  
{
HRESULT hr;
IPropertyStore *pStore;
hr = pDevice->OpenPropertyStore(STGM_READ, &pStore);
if (SUCCEEDED(hr))
{
PROPERTYKEY Drvidkey ={0xb3f8fa53, 0x0004, 0x438e, 0x90, 0x03, 0x51, 0xa4, 0x6e, 0x13, 0x9b, 0xfc, 2};
PROPVARIANT pDrvidkey;
PropVariantInit(&pDrvidkey);
hr = pStore->GetValue(Drvidkey , &pDrvidkey);  
if (SUCCEEDED(hr))
{
wcscpy(DeviceDsc,pDrvidkey.pwszVal);
PropVariantClear(&pDrvidkey);
pStore->Release();
return true;
}
pStore->Release();
}
return false;
}
// 验证设备是否指定设备
bool VerifyDev(IMMDevice *pDevice,EDataFlow dataFlow)
{
wchar_t DeviceDsc[255];
if (GetDeviceDsc(pDevice,DeviceDsc))
{
// 这里省略判断具体设备的 匹配硬件 如 HDAUDIO\FUNC_01&VEN_10EC&DEV_0888&SUBSYS_14627514&REV_1000
return true;
}
return false;
}
// 获取设备音量
int GetDevicePlayVol(void)
{   
IMMDeviceEnumerator* pEnumerator;  
IMMDeviceCollection* pCollection = NULL;  
IMMDevice *pDevice = NULL;  
IAudioEndpointVolume *pVolumeAPI=NULL;   
UINT deviceCount = 0;  
HRESULT hr;
float fVolume = -1;  
CoInitializeEx( NULL , COINIT_MULTITHREADED );  
//实例化 MMDeviceEnumerator 枚举器
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),(void**)&pEnumerator);  
if (hr != S_OK)   
{   
goto FreeEnumerator;
}   
// 枚举 设备到设备容器 eRander:放音设备,DEVICE_STATE_ACTIVE 为当前已激活的设备,禁用和无连接的用其他状态参数
hr = pEnumerator->EnumAudioEndpoints( eRender , DEVICE_STATE_ACTIVE , &pCollection );  
if (hr != S_OK)   
{      
goto FreeCollection;
}   
// 设备容器里的总数
hr = pCollection->GetCount(&deviceCount);   
if (hr != S_OK)   
{   
goto FreeCollection;
}   
for (UINT dev=0; devItem(dev,&pDevice);   
if (hr == S_OK)   
{
if (VerifyDev(pDevice,eRender))
{    // 用 pDevice 的 Activate 方法初始一个 IAudioEndpointVolume 接口
hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),CLSCTX_ALL,NULL,(void **)(&pVolumeAPI));   
// 使用 IAudioEndpintVolume 的方法获取音量,设置音量,设置静音等
hr = pVolumeAPI->GetMasterVolumeLevelScalar(&fVolume);
break;
}
}   
}  
FreeCollection:
SAFE_RELEASE(pCollection);
FreeEnumerator:
SAFE_RELEASE(pEnumerator);
CoUninitialize();
if (fVolume > 0)
return fVolume*100;
else
return fVolume;
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-68523-1-1.html 上篇帖子: win7 64位安装ie9,4个补丁已安装,仍提示“安装程序无法启动” 下篇帖子: win7和win8如何设置快速启动栏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表