如何控制特定应用的音量?

时间:2015-11-19 16:50:35

标签: c++ winapi volume

我是用C ++编写可用于控制音量的代码(例如,Google Chrome),我将该函数添加到我的代码中,但它控制的是主音量而不仅仅是应用程序音量:

setChangeVolume(const double & iVolume)
  {
      HRESULT hr=NULL;
      double newVolume = iVolume;

      CoInitialize(NULL);
      IMMDeviceEnumerator *deviceEnumerator = NULL;
      hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, 
          __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
      IMMDevice *defaultDevice = NULL;

      hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
      deviceEnumerator->Release();
      deviceEnumerator = NULL;

      IAudioEndpointVolume *endpointVolume = NULL;
      hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), 
          CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
      defaultDevice->Release();
      defaultDevice = NULL;

      // -------------------------
      float currentVolume = 0;
      unsigned int icurrentVolume = 0;
      endpointVolume->GetMasterVolumeLevel(&currentVolume);

      hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
      hr = endpointVolume->SetMasterVolumeLevelScalar(static_cast<float>(newVolume), NULL);

      endpointVolume->Release();

      CoUninitialize();
}

你有什么问题吗?如果可能的话? 感谢

0 个答案:

没有答案