Search code examples
naudio

NAudio: Playing many audio files while keeping the sound card opened


In my app I have to play many short audio files, which all have the same sample rate and channels count. I don’t want to constantly open and close the sound card.

I can open it once, and then call WaveOut.Init followed by WaveOut.Play for each file. Or I can use a MixingSampleProvider as explained here (this allows me to play many files simultaneously, but I don’t need that).

Does one approach is better than the other? What are the advantages/disadvantages of each one? And last: Does leaving the sound card opened may conflict with other applications?


Solution

  • If you are playing files with different sample rates and channel counts with WaveOut it's going to be easier just to create a new instance of WaveOut for each one. Otherwise you'd need to resample all your files to the same sample rate with something like MediaFoundationResampler.

    If all your files are the same sample rate and channel count it might be easier to keep one output device open the whole time. A good example of when I recommend doing this is if you are playing sound effects in a game. Have a never-ending MixingSampleProvider that is playing continuously and then add the sounds as they need to be played. This is particularly helpful for short sounds.