NAudio – How to get the volume level of what is playing through the current default device

Posted by Blake on 12/3/2013
)

I was wanting to obtain the current volume level of what was playing in order to code some events that I wanted to occur when something played through the soundcard. My old solution didn’t work on newer computers that didn’t have “What you hear” as a device option so I turned to NAudio where is was very easy. This is basic but here it is:

VB.Net

    Dim devEnum As New NAudio.CoreAudioApi.MMDeviceEnumerator
    Dim defaultDevice As NAudio.CoreAudioApi.MMDevice = devEnum.GetDefaultAudioEndpoint(NAudio.CoreAudioApi.DataFlow.Render, NAudio.CoreAudioApi.Role.Multimedia)
    Dim leftVolume As Integer = defaultDevice.AudioMeterInformation.PeakValues(0) * 100
    Dim rightVolume As Integer = defaultDevice.AudioMeterInformation.PeakValues(1) * 100

C#

    NAudio.CoreAudioApi.MMDeviceEnumerator devEnum = new NAudio.CoreAudioApi.MMDeviceEnumerator();
    NAudio.CoreAudioApi.MMDevice defaultDevice = devEnum.GetDefaultAudioEndpoint(NAudio.CoreAudioApi.DataFlow.Render, NAudio.CoreAudioApi.Role.Multimedia);
    int leftVolume = defaultDevice.AudioMeterInformation.PeakValues(0) * 100;
    int rightVolume = defaultDevice.AudioMeterInformation.PeakValues(1) * 100;

(Note, the C# may need to cast to int on the last two lines).