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

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  {
  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(¤tVolume);
  //printf("Current volume in dB is: %f\n", currentVolume);
 
  _hrStatus = endpointVolume->GetMasterVolumeLevelScalar(¤tVolume);
  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(¤tVolume);
  //printf("Current volume in dB is: %f\n", currentVolume);
 
  _hrStatus = endpointVolume->GetMasterVolumeLevelScalar(¤tVolume);
  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;
}

你可能感兴趣的:(获得与设置系统默认设备主音量音量大小以及静音控制函数)