/// <summary> /// Increments or decrements the current volume level by the <see cref="stepAmount"/>. /// </summary> /// <param name="stepAmount">Value between -100 and 100 indicating the desired step amount. Use negative numbers to decrease /// the volume and positive numbers to increase it.</param> /// <returns>the new volume level assigned</returns> public static float StepMasterVolume(float stepAmount) { IAudioEndpointVolume masterVol = null; try { masterVol = GetMasterVolumeObject(); if (masterVol == null) { return(-1); } float stepAmountScaled = stepAmount / 100; // Get the level float volumeLevel; masterVol.GetMasterVolumeLevelScalar(out volumeLevel); // Calculate the new level float newLevel = volumeLevel + stepAmountScaled; newLevel = Math.Min(1, newLevel); newLevel = Math.Max(0, newLevel); masterVol.SetMasterVolumeLevelScalar(newLevel, Guid.Empty); // Return the new volume level that was set return(newLevel * 100); } finally { if (masterVol != null) { Marshal.ReleaseComObject(masterVol); } } }
/// <summary> /// Get the current system volume, if an error is encountered return the default 100. /// </summary> /// <returns></returns> private static int GetVolumeWindowsVista() { object enumerator = null; Guid devEnumeratorGuid = new Guid("BCDE0395-E52F-467C-8E3D-C4579291692E"); Guid idevEnumeratorGuid = new Guid("00000000-0000-0000-C000-000000000046"); int CLSCTX_INPROC_SERVER = 1; CoInitialize(null); int result = CoCreateInstance(ref devEnumeratorGuid, null, CLSCTX_INPROC_SERVER, ref idevEnumeratorGuid, out enumerator); if (result != 0 || enumerator == null) { return(100); } IMMDeviceEnumerator devEnum = enumerator as IMMDeviceEnumerator; if (devEnum == null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum); return(100); } IntPtr devicePtr = IntPtr.Zero; int eRender = 0; int eConsole = 0; int ret = devEnum.GetDefaultAudioEndpoint(eRender, eConsole, ref devicePtr); if (ret != 0) { System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum); return(100); } object obj = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(devicePtr); IMMDevice device = obj as IMMDevice; if (device == null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum); System.Runtime.InteropServices.Marshal.ReleaseComObject(device); } Guid iAudioEndPointVolGuid = new Guid("5CDF2C82-841E-4546-9722-0CF74078229A"); uint context = (uint)0x17; IntPtr activationParams = IntPtr.Zero; IntPtr endPoint = IntPtr.Zero; ret = device.Activate(ref iAudioEndPointVolGuid, context, activationParams, ref endPoint); if (ret != 0) { System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum); System.Runtime.InteropServices.Marshal.ReleaseComObject(device); return(100); } obj = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(endPoint); IAudioEndpointVolume audioEndpoint = obj as IAudioEndpointVolume; if (audioEndpoint == null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum); System.Runtime.InteropServices.Marshal.ReleaseComObject(device); return(100); } float volume = 0.0f; audioEndpoint.GetMasterVolumeLevelScalar(ref volume); System.Runtime.InteropServices.Marshal.ReleaseComObject(audioEndpoint); System.Runtime.InteropServices.Marshal.ReleaseComObject(device); System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum); return((int)(volume * 100f)); }