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

Stop one song and start another - Both songs play at once

Mar 11, 2016 at 6:14 AM
I'm using WaveOut and AudioFileReader to play MP3 files.

I can start playing a song using the following code, attached to my "Play" button.
void StartPlaying()
{
            _audioFileReader = new AudioFileReader(GetRandomSongFilename());
            _waveOutDevice.Init(_audioFileReader);
            _waveOutDevice.Play();
}
This works fine.

I added a "Stop" button which runs the following code:
void StopPlaying()
{
            _waveOutDevice.Stop();
}
This works fine too. The song stops playing.

I can now click my "Play" button again, and it chooses another song and it starts playing. So far, so good.

Next, I added a "Skip" button, which should skip to the next song. It runs the following code:
void SkipTrack()
{
            StopPlaying();
            StartPlaying();
}
When I run this code, both the current song and the new song play at the same time!

I'm not quite sure why when StopPlaying() and StartPlaying() are activated by two separate button clicks, everything works fine, and only the second song plays, but if they are activated by the same button click, then both songs play.

Interestingly enough, I tried adding a call to Application.DoEvents() in between the StopPlaying() and StartPlaying(), and that DID fix the problem. However, this is not a good solution because I am now moving this code to a non-Winforms application, so I don't have access to Application.DoEvents().

How else can I fix this?

-Joe
Coordinator
Mar 11, 2016 at 7:46 PM
Playback has not really stopped until you get the PlaybackStopped event. Also Init is not really designed to be called more than once. The safe solution is to create a new WaveOut for the new playback.
Mar 12, 2016 at 2:40 AM
That worked, thanks!