/// <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> /// Gets the current master volume in scalar values (percentage) /// </summary> /// <returns>-1 in case of an error, if successful the value will be between 0 and 100</returns> public static float GetMasterVolume() { Interfaces.IAudioEndpointVolume masterVol = null; try { masterVol = GetMasterVolumeObject(); if (masterVol == null) { return(-1); } float volumeLevel; masterVol.GetMasterVolumeLevelScalar(out volumeLevel); return(volumeLevel * 100); } finally { if (masterVol != null) { Marshal.ReleaseComObject(masterVol); } } }