在Windows 7中获取当前系统音量

3

我该如何在Windows 7中获取当前系统的主音量?

我查阅了谷歌,但每个解决方案都返回类似-14686346这样的值,没有清晰的解释它们的含义。


2
我建议你阅读你的问题并问自己 - 任何人都能理解我的问题并能够回答吗?http://tinyurl.com/so-hints - Oded
请定义“当前声音”。听起来你已经找到了返回离散采样值的解决方案 - 如果你不需要它,那么你需要什么? - driis
你的问题并不特别清楚,不太明白具体问题所在。请重新编辑你的问题并包括以下信息:你想要做什么,确切地说。你已经尝试了哪些东西(一个简短的代码示例或链接到其他网站上展示类似操作的示例将有所帮助)。你尝试的事情没有成功的原因:你期望看到什么,实际结果是什么? - user111013
您想获取当前的音量设置吗? - Cody Gray
这应该会有所帮助,我想。 - Mahesh
我认为这个问题和这个问题是一样的! - Iraklis
4个回答

10

您需要寻找 EndpointVolume API这是Windows Vista中发布的新音频API之一,可用于获取或设置主音量。

由于在Vista之前的Windows版本(即Windows XP)之间对相关基础设施进行了重大更改,因此您无需支持旧版本的Windows,这使得此任务变得相对容易。这可能是现有示例未能正常工作的原因。

在CodeProject上有一个完整的托管包装库可供使用:Vista Core Audio API主音量控制。虽然它可能实现了比您所需更多的功能,但您可以了解从C#应用程序确定主系统音量所需执行的操作。


6
CodeProject的链接已失效。 - NateS

10

既然您将C#作为一个标签,这里有一个小的C#控制台应用程序来获取它。 它基于GetMasterVolumeLevelScalar方法(Vista或更高版本)。

GetMasterVolumeLevelScalar方法获取输入或离开音频终结点设备的音频流的主音量级别。 音量级别表示为规范化的、音频缩放值,在0.0到1.0的范围内。

  class Program
  {
      static void Main(string[] args)
      {
          Console.WriteLine(VolumeUtilities.GetMasterVolume());
      }
  }


  public static class VolumeUtilities
  {
      public static float GetMasterVolume()
      {
          // get the speakers (1st render + multimedia) device
          IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
          IMMDevice speakers;
          const int eRender = 0;
          const int eMultimedia = 1;
          deviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, out speakers);

          object o;
          speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out o);
          IAudioEndpointVolume aepv = (IAudioEndpointVolume)o;
          float volume = aepv.GetMasterVolumeLevelScalar();
          Marshal.ReleaseComObject(aepv);
          Marshal.ReleaseComObject(speakers);
          Marshal.ReleaseComObject(deviceEnumerator);
          return volume;
      }

      [ComImport]
      [Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
      private class MMDeviceEnumerator
      {
      }

      [Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
      private interface IAudioEndpointVolume
      {
          void _VtblGap1_6();
          float GetMasterVolumeLevelScalar();
      }

      [Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
      private interface IMMDeviceEnumerator
      {
          void _VtblGap1_1();

          [PreserveSig]
          int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice ppDevice);
      }

      [Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
      private interface IMMDevice
      {
          [PreserveSig]
          int Activate([MarshalAs(UnmanagedType.LPStruct)] Guid iid, int dwClsCtx, IntPtr pActivationParams, [MarshalAs(UnmanagedType.IUnknown)] out object ppInterface);
      }
  }

9

似乎无法捕获输出,例如使用“getvol.exe > file”时,文件为空。 有什么想法吗? - NateS
这里是问题所在。你能不能加一个flush并重新编译呢?我的mingw32无法构建它,而我不想玩mingw64以破坏我的工作中的mingw32。干杯! - NateS
1
嗯,在这里(vista)似乎工作正常,重定向到一个文件,这个文件有一个额外的刷新:https://sourceforge.net/projects/mplayer-edl/files/current_system_volume_vista_plus_flush.exe/download - rogerdpack
太好了,谢谢!我已经验证了你的新版本现在可以重定向到文件。干杯! - NateS

0
您可以通过Windows Core Audio API获取和设置Windows的音量。 C++程序example
#include <cstdio>
#include <Windows.h>
#include <MMDeviceAPI.h>
#include <EndpointVolume.h>

int main(int argc, char* argv[])
{
    // Initialize the COM library
    CoInitialize(NULL);  

    IMMDeviceEnumerator* pEnumerator;
    IMMDevice* pDevice;
    IAudioEndpointVolume* pEndpointVolume;

    // Create a device enumerator
    CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnumerator);

    // Get the default audio endpoint device
    pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);

    // Activate the IAudioEndpointVolume interface
    pDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&pEndpointVolume);

    if ( argc == 1 ) {
        // Get the master volume level
        float currentVolume;
        pEndpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
        printf("Current sound volume: %.1f\n\n", currentVolume);
    }

    if ( argc == 2 ) {
        float volume = strtof(argv[1], NULL);
        // Verify valid input range 0~1
        if ( ! ( 0.f <= volume && volume <= 1.f ) )
            return 1;
        pEndpointVolume->SetMasterVolumeLevelScalar(volume, NULL);
    }

    CoUninitialize();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接