Search code examples
c#audionaudio

NAudio - Can not Replay the played sound


Here is my code:

var w = new WaveOut();
var wf = new Mp3FileReader(new MemoryStream(Resources.click));
w.Init(wf);
w.Play();

It will play the audio once, If I Play it again, it will not play it. If I want to make a new Mp3FileReader every time to play the audio, the memory usage of my program will grow more and more.

I just need to use one WaveOut and play the sound in multiple threads as I can. possible?


Solution

  • I have found the solution, we can reset the memory stream position to play the audio again without memory usage:

    audio = new Mp3FileReader(new MemoryStream(Resources.click)); // audio is global
    player = new WaveOut(); // player is also global
    player.Init(audio);
    

    then whenever we need to play the sound we will do this:

    audio.Seek(0, SeekOrigin.Begin);
    player.Play();