Volume quirk with Waveout when Pause/Resume play

Mar 14, 2013 at 7:21 PM
I found a Volume quirk with Waveout and am wondering if there is a work around.

I start a WaveOut playing with 100% volume. Pause it, then set volume to 0%. Then click Resume. I would expect the MP3 to continue playing with no sound. But instead, I get a quick burst of sound (maybe less than a tenth of a second worth), then play continues as expected at volume=0.

So I'm assuming that when I click Resume button, there is a small buffer of audio left in the Waveout buffer. Is that true? Is yes, is there a way to flush that buffer before continuing the play?

IWavePlayer waveOutDevice = null;
string fileName = "myaudio.mp3";
           
WaveStream  mediaFileReader = new Mp3FileReader(fileName);
WaveChannel32   volumeStream = new WaveChannel32(mediaFileReader);
WaveChannel32 volumeStream =  new WaveChannel32(mediaFileReader);
WaveStream mainOutputStream = volumeStream;


waveOutDevice.Init(mainOutputStream);
waveOutDevice.Play();  //start play

volumeStream.Volume = 1;   //vol 100%

...
waveOutDevice.Pause();  //pause
volumeStream.Volume = 0;   //vol 0%
...
waveOutDevice.Play();  //resume, volume burst happens here

Mar 15, 2013 at 3:48 PM
Edited Mar 15, 2013 at 3:48 PM
Well, I solved this by the steps below. If anyone knows of a more elegant solution, I'm all ears.

1) On pause, saving Current position and volume
TimeSpan positionTimeSpan =  mainOutputStream.CurrentTime ;
float volumeOnPause  = currVolume;
2) On resume, if volume has changed since pause, flush the out buffer and restore position
if (volumeOnPause != currVolume)
                {
                    waveOutDevice.Stop();   // flush the buffer, brute force method
                    mainOutputStream.CurrentTime = positionTimeSpan;  // reset position
                }
               
                waveOutDevice.Play();
Coordinator
Mar 15, 2013 at 3:51 PM
yes, calling Stop flushes the buffers. Just because the API is called "Stop", doesn't mean you can't use it when the user presses the "Pause" button.
Mar 15, 2013 at 3:57 PM
Ok thanks Mark. This solution seems to work fine.