开发者

How to globally mute and unmute sound in Vista and 7, and to get a mute state?

I'm using the old good Mixer API right now, but it does not work as expected on Windows Vista & 7 in the normal, not in XP compatibility mode. It mutes the sound for the current app only, but I need a global (hardware) mute. How to rearch the goal? Is there any way to code this w/o COM interfaces and开发者_如何学JAVA strange calls, in pure C/C++?


The audio stack was significantly rewritten for Vista. Per-application volume and mute control was indeed one of the new features. Strange calls will be required to use the IAudioEndpointVolume interface.


I recently dealt with this same issue. We have a Windows application that uses the sound system for alarms. We cannot abide the user muting the sound system inadvertently. Here is how I was able to use the interface suggested above to address this issue:

During initialization I added a function to initialize a member of type IAudioEndpointVolume. It was a bit tricky and the help wasn't as helpful as it could be. Here's how to do it:

/****************************************************************************
**  Initialize the Audio Endpoint (Only for post XP systems)
****************************************************************************/
void CMuteWatchdog::InitAudioEndPoint(void)
{
   HRESULT hr;
   IMMDeviceEnumerator * pDevEnum;
   IMMDevice * pDev;

   const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
   const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);

   hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL,
      CLSCTX_ALL, IID_IMMDeviceEnumerator,
      (void**)&pDevEnum);

   m_pIaudEndPt = NULL;

   if(hr == S_OK)
   {
      hr = pDevEnum->GetDefaultAudioEndpoint(eRender, eConsole, &pDev);
      if(hr == S_OK)
      {
         DWORD dwClsCtx;
         const IID iidAEV = __uuidof(IAudioEndpointVolume);

         dwClsCtx = 0;

         hr = pDev->Activate(iidAEV, dwClsCtx, NULL, (void**) &m_pIaudEndPt);

         if(hr == S_OK)
         {
            // Everything is groovy.
         }
         else
         {
            m_pIaudEndPt = NULL; // Might mean it's running on XP or something.  Don't use.
         }

         pDev->Release();
      }
      pDevEnum->Release();
   }
}

...

About once per second I added a simple call to the following:

////////////////////////////////////////////////////////////////////////
// Watchdog function for mute.
void CMuteWatchdog::GuardMute(void)
{
   if(m_pIaudEndPt)
   {
      BOOL bMute;
      HRESULT hr;

      bMute = FALSE;

      hr = m_pIaudEndPt->GetMute(&bMute);

      if(hr == S_OK)
      {
         if(bMute)
         {
            m_pIaudEndPt->SetMute(FALSE, NULL);
         }
      }
   }
}

Finally, when the program exits just remember to release the allocated resource.

////////////////////////////////////////////////////////////////////////
// De-initialize the watchdog
void CMuteWatchdog::OnClose(void)
{
   if(m_pIaudEndPt)
   {
      m_pIaudEndPt->Release();
      m_pIaudEndPt = NULL;
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜