This project has moved. For the latest updates, please go here.

Detect current volume level while a file is playing using Naudio

Oct 30, 2012 at 8:49 PM
Edited Oct 30, 2012 at 8:50 PM

So I have this code. There is a Stop-Button ( button1 ), which stops playback. When the form is loaded, the file is played. While the file is playing I want to get the current volume of the file by running a function.

        IWavePlayer waveOutDevice;
        WaveStream mainOutputStream;
        WaveChannel32 volumeStream;

        private WaveStream CreateInputStream(string fileName)
        {
            WaveChannel32 inputStream;
            if (fileName.EndsWith(".mp3"))
            {
                WaveStream mp3Reader = new Mp3FileReader(fileName);
                inputStream = new WaveChannel32(mp3Reader);
            }
            else
            {
                throw new InvalidOperationException("Unsupported extension");
            }
            volumeStream = inputStream;
            return volumeStream;
        }

        private void Stop()
        {
            if (waveOutDevice != null)
            {
                waveOutDevice.Stop();
            }
            if (mainOutputStream != null)
            {
                // this one really closes the file and ACM conversion
                volumeStream.Close();
                volumeStream = null;
                // this one does the metering stream
                mainOutputStream.Close();
                mainOutputStream = null;
            }
            if (waveOutDevice != null)
            {
                waveOutDevice.Dispose();
                waveOutDevice = null;
            }
        }

        private void Play(string was)
        {
            waveOutDevice = new WaveOut();
            mainOutputStream = CreateInputStream(was);
            waveOutDevice.Init(mainOutputStream);
            waveOutDevice.Play();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Play(@"E:\Eigene Audiodateien\Musik\Alben\Pur\Abenteuerland\ -  - .mp3");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stop();
        }

So what does a function like this has to look like at "...."?

    private int currentVolumeLevel(...some suitable parameters...)
    {
      int currentVolumeLevelValue = 0;
      //....
      return currentVolumeLevelValue;
    }

I am not talking about the volume level you can adjust with windows' sound controls. I am talking about the currently played sound file's volume at this very position it is playing right now, based on something like a byte[] array.

Thanks in advance.

Coordinator
Oct 31, 2012 at 7:23 AM

The NAudioDemo shows how to do this (see AudioPlaybackPanel.cs). You can use a MeteringSampleProvider to raise regular events with the maximum sample value.

Oct 31, 2012 at 10:22 AM

I do not understand your sample. Please create the function currentVolumeLevel for us.