repeat playback of a sound file with Naudio

May 25, 2011 at 8:01 PM

I have a function that allows me play a sound file located in a database in all sound cards installed in my pc.I uses Naudio  library.
How can I repeat reading this file automatically and thank you in advance for your helpers.Here is my code:

private void PlaySoundInDevice(int deviceNumber, string fileName)
if (outputDevices.ContainsKey(deviceNumber))
var waveOut = new WaveOut();
waveOut.DeviceNumber = deviceNumber;
WaveStream waveReader = new WaveFileReader(fileName);
// hold onto the WaveOut and WaveStream so we can dispose them later
outputDevices[deviceNumber] = new PlaybackSession { WaveOut = waveOut, WaveStream = waveReader };


private Dictionary outputDevices = new Dictionary();

class PlaybackSession
public IWavePlayer WaveOut { get; set; }
public WaveStream WaveStream { get; set; }
private void DisposeAll()
foreach (var playbackSession in outputDevices.Values)
public void PlayInAllAvailableDevices(string fileName)
int waveOutDevices = WaveOut.DeviceCount;
for (int n = 0; n < waveOutDevices; n++)
PlaySoundInDevice(n, fileName);

May 26, 2011 at 6:11 AM

Here's a tutorial on looped playback with NAudio:

May 26, 2011 at 1:13 PM

Thanks markheath for your rely.

I have another problem. My project allows also broadcast a direct voice message .I want to add a chime function before the star tof diffusion the message.It  is a
function that attracts people's attention like the tone we listen  in an airport (ding dong).I don't know how I can do. Will you help me to to do this function.

May 26, 2011 at 1:21 PM

It's the same approach. Create a class that derives from WaveStream (actually IWaveProvider is easier). In the constructor take the two source IWaveProviders that you want to play one after the other. Then in the Read method, once you have read all from the first one, start reading from the next.

Something like this (not tested, just coded off the top of my head)

int Read(byte[] buffer, int offset, int length)
   int totalRead = 0;
   if (!readAllFromFirst)
       totalRead = first.Read(buffer,offset,length);
       if (totalRead < length) readAllFromFirst = true;
       offset += totalRead;
       length -= totalRead;
   if (readAllFromFirst)
      totalRead += second.Read(buffer,offset,length);
   return totalRead;

May 26, 2011 at 1:34 PM


The problem I haven't  a file sound to read.  When debugging  I express my message.

May 26, 2011 at 1:41 PM

I'm not quite sure what you mean. You would be best off finding a WAV file with a chime sound that you like.

May 26, 2011 at 1:50 PM


I  want to say that the second sound broadcast not a sound file but a live voice message that I expressed when debugging.
I do not know how to use your code for this and thanks for your help markheath

May 26, 2011 at 1:53 PM

Well in that case, "second" would be a buffered wave provider.

May 26, 2011 at 1:58 PM


I'll try to work with your idea and I tell you that there is a resulting output  and thank you again.