/// <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) { Interfaces.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> /// Sets the master volume to a specific level /// </summary> /// <param name="newLevel">Value between 0 and 100 indicating the desired scalar value of the volume</param> public static void SetMasterVolume(float newLevel) { Interfaces.IAudioEndpointVolume masterVol = null; try { masterVol = GetMasterVolumeObject(); if (masterVol == null) { return; } masterVol.SetMasterVolumeLevelScalar(newLevel / 100, Guid.Empty); } finally { if (masterVol != null) { Marshal.ReleaseComObject(masterVol); } } }