Search code examples
naudio

NAudio: Should I dispose a WaveOut object before each call to the Init method


I once read in a tutorial (unfortunately, I can't find where), that if I want to play several audio files, and use WaveOut, I should dispose the last WaveOut object before calling the Init method again. If not, I will get an error. However, I didn't dispose the objects, and everything works. I did this:

Mp3FileReader reader_1 = new Mp3FileReader("1.mp3");
Mp3FileReader reader_2 = new Mp3FileReader("2.mp3");

WaveOut WaveOutDevice = new WaveOut();

WaveOutDevice.Init(reader_1);
WaveOutDevice.Play();

and after a while, playing a second file:

WaveOutDevice.Init(reader_2);
WaveOutDevice.Play();

So I just want to be sure that I'm doing the right thing.


Solution

  • WaveOut was initially designed for Init to only be called a single time. In some circumstances it may work, but I'd recommend creating a new instance of WaveOut to play your next sound.