如何在 Vista 和 7 中全局静音和取消静音,并获得静音状态?

发布于 2024-08-23 07:48:09 字数 162 浏览 6 评论 0原文

我现在正在使用旧的、良好的 Mixer API,但它在 Windows Vista 和 Windows 上无法按预期工作。 7.在正常情况下,在XP兼容模式下不行。它仅使当前应用程序静音,但我需要全局(硬件)静音。如何达到目标?有没有什么方法可以用纯 C/C++ 编写这个代码,无需 COM 接口和奇怪的调用?

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 strange calls, in pure C/C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

会傲 2024-08-30 07:48:09

Vista 的音频堆栈被大幅重写。每个应用程序的音量和静音控制确实是新功能之一。使用 IAudioEndpointVolume 接口。

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.

朦胧时间 2024-08-30 07:48:09

我最近处理了同样的问题。我们有一个使用声音系统进行警报的 Windows 应用程序。我们不能容忍用户无意中将音响系统静音。以下是我如何使用上面建议的接口来解决此问题:

在初始化期间,我添加了一个函数来初始化 IAudioEndpointVolume 类型的成员。这有点棘手,而且帮助并没有发挥应有的帮助。下面是如何做到这一点:

/****************************************************************************
**  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();
   }
}

...

大约每秒一次,我添加了对以下内容的简单调用:

////////////////////////////////////////////////////////////////////////
// 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);
         }
      }
   }
}

最后,当程序退出时,请记住释放分配的资源。

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

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;
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文