获得与设置系统默认设备主音量音量大小以及静音控制函数

CoreApi::CoreApi(void)
{
 CoInitializeEx(NULL, COINIT_MULTITHREADED); 
}

CoreApi::~CoreApi(void)
{
 
}
bool CoreApi::InitDeviceIds(void)
{
 CString str;
 CString str_temp;

 IMMDeviceEnumerator* pEnumerator;  

 IMMDeviceCollection *pCollection = NULL;  

 IMMDevice *pDevice=NULL;  

 IPropertyStore *pProperties=NULL;  

 

 UINT deviceCount = 0;  

 bool bFindDev_master=false;
 bool bFindDev_spdif=false;

 //LPWSTR pwszID = NULL;

 // CoInitializeEx(NULL, COINIT_MULTITHREADED);  
 HRESULT hr=CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),(void**)&pEnumerator);
 EXIT_ON_ERROR(hr);

// CMMNotificationClient  *pNotify=new CMMNotificationClient;
// pEnumerator->RegisterEndpointNotificationCallback(pNotify);


 hr = pEnumerator->EnumAudioEndpoints( eAll/*eCapture eRender*/, DEVICE_STATE_ACTIVE, &pCollection);  
 EXIT_ON_ERROR(hr);

 hr = pCollection->GetCount(&deviceCount);  
 EXIT_ON_ERROR(hr);

 str_temp.Format(" deviceCount = %d !\n",deviceCount);
 str+=str_temp;

// AfxMessageBox(str);
 str.Empty();

 for (UINT dev=0;dev<deviceCount;dev++)
 {
  pDevice = NULL;  
  hr = pCollection->Item(dev,&pDevice);

  //hr = pDevice->GetId(&pwszID);
  //枚举设备号和名字
  if (hr == S_OK)  
  {
   
   hr = pDevice->OpenPropertyStore(STGM_READ,&pProperties);
   if (hr == S_OK)  
   {     
    
    PROPVARIANT varName;  
    PropVariantInit(&varName);
    hr = pProperties->GetValue(PKEY_Device_FriendlyName, &varName);
    
    //str.Format("Endpoint %d: \"%S\" (%S)\n",dev, varName.pwszVal, pwszID);
    //AfxMessageBox(str);

    if (hr == S_OK)
    {
     str_temp.Format("device index = %d \n",dev);
     str+=str_temp;
     str+=varName.pwszVal;
     str+="\n";

     str_temp=varName.pwszVal;
     str_temp.MakeLower();
     if (str_temp.Find("hifier")>-1)
     {
      
      /*
      if ((str_temp.Find("扬声器")>-1)||(str_temp.Find("揚聲器")>-1))
            {
             bFindDev_master=true;
             pDevice->GetId(&deviceId_out_master);
            }*/
      if ((str_temp.Find("数字输出")>-1)||(str_temp.Find("pdif")>-1)||(str_temp.Find("through")>-1))
      {
       bFindDev_spdif=true;
       pDevice->GetId(&deviceId_out_spdif);
      }
      
     }
    }
    //CoTaskMemFree(pwszID);
    //pwszID = NULL;

    PropVariantClear(&varName);
   }

   SAFE_RELEASE(pProperties);

 

  }

 // AfxMessageBox(str);//遍历一个设备,显示一次信息
  str.Empty();
  /*
  if (dev==(deviceCount-1))
    {
     SAFE_RELEASE(pEnumerator);
     SAFE_RELEASE(pCollection);
     return true;
    }*/
  

 }
 if (bFindDev_master&&bFindDev_spdif)
 {
  SAFE_RELEASE(pEnumerator);
  SAFE_RELEASE(pCollection);
  return true;

 }

 

Exit:
 SAFE_RELEASE(pEnumerator);
 SAFE_RELEASE(pCollection);
 return false;
}

 

HRESULT CoreApi::GetMasterVol(float  *pfLevel)
{
 IMMDeviceEnumerator  *pEnumerator = NULL;
 IMMDevice  *pDevice = NULL;
 HRESULT  _hrStatus = S_OK;
 IAudioSessionManager  *_pManager = NULL;
 IAudioSessionControl  *_pControl = NULL;
 ISimpleAudioVolume  *pAudioVolume = NULL;

 _hrStatus = CoCreateInstance(__uuidof(MMDeviceEnumerator),
  NULL, CLSCTX_INPROC_SERVER,
  __uuidof(IMMDeviceEnumerator),
  (void**)&pEnumerator);
 EXIT_ON_ERROR(_hrStatus)

 _hrStatus = pEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia,&pDevice);
 EXIT_ON_ERROR(_hrStatus)

  // Get the session manager for the endpoint device.
 /*
 _hrStatus = pDevice->Activate(__uuidof(IAudioSessionManager),
   CLSCTX_INPROC_SERVER, NULL,(void**)&_pManager);
  EXIT_ON_ERROR(_hrStatus)
 
  _hrStatus = _pManager->GetAudioSessionControl(NULL, 0, &_pControl);
  EXIT_ON_ERROR(_hrStatus)
 
  _hrStatus = _pManager->GetSimpleAudioVolume(NULL,0,&pAudioVolume);
  EXIT_ON_ERROR(_hrStatus)
 
  pAudioVolume->GetMasterVolume(pfLevel);*/
  IAudioEndpointVolume *endpointVolume = NULL;
 _hrStatus = pDevice->Activate(__uuidof(IAudioEndpointVolume),
  CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
 EXIT_ON_ERROR(_hrStatus)
 //defaultDevice->Release();
 //defaultDevice = NULL;

 // -------------------------
 //float currentVolume = 0;
 _hrStatus = endpointVolume->GetMasterVolumeLevel(pfLevel);
 EXIT_ON_ERROR(_hrStatus)
 endpointVolume->Release();
 /*
 endpointVolume->GetMasterVolumeLevel(&currentVolume);
  //printf("Current volume in dB is: %f\n", currentVolume);
 
  _hrStatus = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
  EXIT_ON_ERROR(_hrStatus)*/
 

Exit:
 SAFE_RELEASE(pEnumerator)
  SAFE_RELEASE(pDevice)


 return true;
}
HRESULT CoreApi::SetMasterVol(float  fLevel,LPCGUID  EventContext)
{
 IMMDeviceEnumerator  *pEnumerator = NULL;
 IMMDevice  *pDevice = NULL;
 HRESULT  _hrStatus = S_OK;
 IAudioSessionManager  *_pManager = NULL;
 IAudioSessionControl  *_pControl = NULL;
 ISimpleAudioVolume  *pAudioVolume = NULL;

 _hrStatus = CoCreateInstance(__uuidof(MMDeviceEnumerator),
  NULL, CLSCTX_INPROC_SERVER,
  __uuidof(IMMDeviceEnumerator),
  (void**)&pEnumerator);
 EXIT_ON_ERROR(_hrStatus)

  _hrStatus = pEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia,&pDevice);
 EXIT_ON_ERROR(_hrStatus)

  // Get the session manager for the endpoint device.
  /*
  _hrStatus = pDevice->Activate(__uuidof(IAudioSessionManager),
    CLSCTX_INPROC_SERVER, NULL,(void**)&_pManager);
   EXIT_ON_ERROR(_hrStatus)
  
    _hrStatus = _pManager->GetAudioSessionControl(NULL, 0, &_pControl);
   EXIT_ON_ERROR(_hrStatus)
  
    _hrStatus = _pManager->GetSimpleAudioVolume(NULL,0,&pAudioVolume);
   EXIT_ON_ERROR(_hrStatus)
  
    pAudioVolume->SetMasterVolume(fLevel,EventContext);*/
  

 IAudioEndpointVolume *endpointVolume = NULL;
 _hrStatus = pDevice->Activate(__uuidof(IAudioEndpointVolume),
  CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
 EXIT_ON_ERROR(_hrStatus)
 //defaultDevice->Release();
 //defaultDevice = NULL;

 // -------------------------
 //float currentVolume = 0;
 _hrStatus = endpointVolume->SetMasterVolumeLevel(fLevel,EventContext);
 EXIT_ON_ERROR(_hrStatus)
 endpointVolume->Release();
 /*
 endpointVolume->GetMasterVolumeLevel(&currentVolume);
  //printf("Current volume in dB is: %f\n", currentVolume);
 
  _hrStatus = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
  EXIT_ON_ERROR(_hrStatus)*/
 
 //CString strCur=L"";
 //strCur.Format(L"%f",currentVolume);
 //AfxMessageBox(strCur);

 // printf("Current volume as a scalar is: %f\n", currentVolume);
 /*
 if (bScalar==false)
  {
   _hrStatus = endpointVolume->SetMasterVolumeLevel((float)newVolume, NULL);
   EXIT_ON_ERROR(_hrStatus)
  }
  else if (bScalar==true)
  {
   _hrStatus = endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL);
   EXIT_ON_ERROR(_hrStatus)
  }*/
 

Exit:
 SAFE_RELEASE(pEnumerator)
  SAFE_RELEASE(pDevice)


 return true;
}

HRESULT CoreApi::GetMasterVolId()
{
 IMMDeviceEnumerator  *pEnumerator = NULL;
 IMMDevice  *pDevice = NULL;
 HRESULT  _hrStatus = S_OK;
 IAudioEndpointVolume *endpointVolume = NULL;

 _hrStatus = CoCreateInstance(__uuidof(MMDeviceEnumerator),
  NULL, CLSCTX_INPROC_SERVER,
  __uuidof(IMMDeviceEnumerator),
  (void**)&pEnumerator);
 EXIT_ON_ERROR(_hrStatus)

  _hrStatus = pEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia,&pDevice);
 EXIT_ON_ERROR(_hrStatus)

 _hrStatus = pDevice->Activate(__uuidof(IAudioEndpointVolume),
  CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
 EXIT_ON_ERROR(_hrStatus)

 _hrStatus = pDevice->GetId(&deviceId_out_master);
 EXIT_ON_ERROR(_hrStatus)
 endpointVolume->Release();
 

Exit:
 SAFE_RELEASE(pEnumerator)
 SAFE_RELEASE(pDevice)


 return true;
}

HRESULT CoreApi::GetMasterChannelVol(UINT nChannel,float  *pfLevel)
{
 IMMDeviceEnumerator  *pEnumerator = NULL;
 IMMDevice  *pDevice = NULL;
 HRESULT  _hrStatus = S_OK;
 IAudioSessionManager  *_pManager = NULL;
 IAudioSessionControl  *_pControl = NULL;
 ISimpleAudioVolume  *pAudioVolume = NULL;

 _hrStatus = CoCreateInstance(__uuidof(MMDeviceEnumerator),
  NULL, CLSCTX_INPROC_SERVER,
  __uuidof(IMMDeviceEnumerator),
  (void**)&pEnumerator);
 EXIT_ON_ERROR(_hrStatus)

  _hrStatus = pEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia,&pDevice);
 EXIT_ON_ERROR(_hrStatus)

  // Get the session manager for the endpoint device.
  /*
 _hrStatus = pDevice->Activate(__uuidof(IAudioSessionManager),
   CLSCTX_INPROC_SERVER, NULL,(void**)&_pManager);
  EXIT_ON_ERROR(_hrStatus)
 
   _hrStatus = _pManager->GetAudioSessionControl(NULL, 0, &_pControl);
  EXIT_ON_ERROR(_hrStatus)
 
   _hrStatus = _pManager->GetSimpleAudioVolume(NULL,0,&pAudioVolume);
  EXIT_ON_ERROR(_hrStatus)
 
  pAudioVolume->GetMute(pbMute);*/

 IAudioEndpointVolume *endpointVolume = NULL;
 _hrStatus = pDevice->Activate(__uuidof(IAudioEndpointVolume),
  CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
 EXIT_ON_ERROR(_hrStatus)

 _hrStatus = endpointVolume->GetChannelVolumeLevelScalar(nChannel,pfLevel);
 EXIT_ON_ERROR(_hrStatus)
 endpointVolume->Release();
 

Exit:
 SAFE_RELEASE(pEnumerator)
 SAFE_RELEASE(pDevice)


 return true;
}

HRESULT CoreApi::SetMasterChannelVol(UINT nChannel,float  fLevel,LPCGUID  EventContext)
{
 IMMDeviceEnumerator  *pEnumerator = NULL;
 IMMDevice  *pDevice = NULL;
 HRESULT  _hrStatus = S_OK;
 IAudioSessionManager  *_pManager = NULL;
 IAudioSessionControl  *_pControl = NULL;
 ISimpleAudioVolume  *pAudioVolume = NULL;

 _hrStatus = CoCreateInstance(__uuidof(MMDeviceEnumerator),
  NULL, CLSCTX_INPROC_SERVER,
  __uuidof(IMMDeviceEnumerator),
  (void**)&pEnumerator);
 EXIT_ON_ERROR(_hrStatus)

 _hrStatus = pEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia,&pDevice);
 EXIT_ON_ERROR(_hrStatus)

  // Get the session manager for the endpoint device.
  /*
 _hrStatus = pDevice->Activate(__uuidof(IAudioSessionManager),
   CLSCTX_INPROC_SERVER, NULL,(void**)&_pManager);
  EXIT_ON_ERROR(_hrStatus)
 
   _hrStatus = _pManager->GetAudioSessionControl(NULL, 0, &_pControl);
  EXIT_ON_ERROR(_hrStatus)
 
   _hrStatus = _pManager->GetSimpleAudioVolume(NULL,0,&pAudioVolume);
  EXIT_ON_ERROR(_hrStatus)
 
  pAudioVolume->GetMute(pbMute);*/

 IAudioEndpointVolume *endpointVolume = NULL;
 _hrStatus = pDevice->Activate(__uuidof(IAudioEndpointVolume),
  CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
 EXIT_ON_ERROR(_hrStatus)

 _hrStatus = endpointVolume->SetChannelVolumeLevelScalar(nChannel,fLevel,EventContext);
 EXIT_ON_ERROR(_hrStatus)
 endpointVolume->Release();
 

Exit:
 SAFE_RELEASE(pEnumerator)
 SAFE_RELEASE(pDevice)


 return true;
}

HRESULT CoreApi::GetMasterMute(BOOL  *pbMute)
{
 IMMDeviceEnumerator  *pEnumerator = NULL;
 IMMDevice  *pDevice = NULL;
 HRESULT  _hrStatus = S_OK;
 IAudioSessionManager  *_pManager = NULL;
 IAudioSessionControl  *_pControl = NULL;
 ISimpleAudioVolume  *pAudioVolume = NULL;

 _hrStatus = CoCreateInstance(__uuidof(MMDeviceEnumerator),
  NULL, CLSCTX_INPROC_SERVER,
  __uuidof(IMMDeviceEnumerator),
  (void**)&pEnumerator);
 EXIT_ON_ERROR(_hrStatus)

  _hrStatus = pEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia,&pDevice);
 EXIT_ON_ERROR(_hrStatus)

  // Get the session manager for the endpoint device.
  /*
 _hrStatus = pDevice->Activate(__uuidof(IAudioSessionManager),
   CLSCTX_INPROC_SERVER, NULL,(void**)&_pManager);
  EXIT_ON_ERROR(_hrStatus)
 
   _hrStatus = _pManager->GetAudioSessionControl(NULL, 0, &_pControl);
  EXIT_ON_ERROR(_hrStatus)
 
   _hrStatus = _pManager->GetSimpleAudioVolume(NULL,0,&pAudioVolume);
  EXIT_ON_ERROR(_hrStatus)
 
  pAudioVolume->GetMute(pbMute);*/

 IAudioEndpointVolume *endpointVolume = NULL;
 _hrStatus = pDevice->Activate(__uuidof(IAudioEndpointVolume),
  CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
 EXIT_ON_ERROR(_hrStatus)

 _hrStatus = endpointVolume->GetMute(pbMute);
 EXIT_ON_ERROR(_hrStatus)
  endpointVolume->Release();
 

Exit:
 SAFE_RELEASE(pEnumerator)
  SAFE_RELEASE(pDevice)


 return true;
}
HRESULT CoreApi::SetMasterMute(BOOL  bMute,LPCGUID  EventContext)
{
 IMMDeviceEnumerator  *pEnumerator = NULL;
 IMMDevice  *pDevice = NULL;
 HRESULT  _hrStatus = S_OK;
 IAudioSessionManager  *_pManager = NULL;
 IAudioSessionControl  *_pControl = NULL;
 ISimpleAudioVolume  *pAudioVolume = NULL;

 _hrStatus = CoCreateInstance(__uuidof(MMDeviceEnumerator),
  NULL, CLSCTX_INPROC_SERVER,
  __uuidof(IMMDeviceEnumerator),
  (void**)&pEnumerator);
 EXIT_ON_ERROR(_hrStatus)

 _hrStatus = pEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia,&pDevice);
 EXIT_ON_ERROR(_hrStatus)

  // Get the session manager for the endpoint device.
  /*
 _hrStatus = pDevice->Activate(__uuidof(IAudioSessionManager),
   CLSCTX_INPROC_SERVER, NULL,(void**)&_pManager);
  EXIT_ON_ERROR(_hrStatus)
 
   _hrStatus = _pManager->GetAudioSessionControl(NULL, 0, &_pControl);
  EXIT_ON_ERROR(_hrStatus)
 
   _hrStatus = _pManager->GetSimpleAudioVolume(NULL,0,&pAudioVolume);
  EXIT_ON_ERROR(_hrStatus)
 
  pAudioVolume->SetMute(bMute,EventContext);*/

 IAudioEndpointVolume *endpointVolume = NULL;
 _hrStatus = pDevice->Activate(__uuidof(IAudioEndpointVolume),
  CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
 EXIT_ON_ERROR(_hrStatus)

 _hrStatus = endpointVolume->SetMute(bMute,EventContext);
 EXIT_ON_ERROR(_hrStatus)
 endpointVolume->Release();
 

Exit:
 SAFE_RELEASE(pEnumerator)
 SAFE_RELEASE(pDevice)


 return true;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值