Get current system volume in Windows 7

How can I get the current main system volume in Windows 7?

I searched Google, but each solution returned values ​​such as -1or 4686346without a clear explanation of what they mean.

+3
source share
3 answers

You are looking for an EndpointVolumeAPI . . This is part of the new audio APIs that were released in Windows Vista and can be used to obtain or install the primary volume.

, Windows Vista ( Windows XP), . , , , , .

CodeProject : Windows Core Audio. , , , , , , #.

+8

, , ++, , API IAudioEndpointVolume.

, "" , 0 100 ( ).

: https://gist.github.com/rdp/8363580

.

. fooobar.com/questions/1784386/...

+8

# , #, . GetMasterVolumeLevelScalar (Vista ).

The GetMasterVolumeLevelScalar method gets the master level level of the audio stream that enters or leaves the audio endpoint device. The volume level is expressed as a normalized, audio-cone value in the range from 0.0 to 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);
      }
  }
+4
source

Source: https://habr.com/ru/post/1784383/


All Articles