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

Streaming Audio Data with WaveOutDevice and a WaveProvider

Oct 5, 2014 at 5:01 AM
Hi,

I decide to use NAudio for my project because of its well equipped feature set and high-level access to audio data, and their respective endpoint devices. I'm sending uncompressed PCM data in small packets over-the-wire by UDP Protocol(thanks to Lidgren), and am attempting to store it in a buffered wave provider and feed it to the output device, however I'm a bit stumped regarding how to set up my code, so that I'm only feeding the buffer, and not reinitializing the audio device to the buffer, every time it is filled with new PCM data.

How I'm Handling Audio Data

case MessageType.Audio_ByteData:
              int count = msg.ReadInt32();
              msg.SkipPadBits();
              byte[] audioData = msg.ReadBytes(count);
              bufferedWaveProvider.AddSamples(audioData, 0, audioData.Length);
              outputDevice.Init(bufferedWaveProvider);
              outputDevice.Play();
                                  
              break;
As of now, I'm only sending over 1 second of audio data for debugging purposes, but the o nly time I seem to be able to listen to the PCM data I sent over is by initializing and calling play right after I add samples to the buffered wave provider. I've tried, adding samples while creating a busy loop of outputDevice.Play(), on another thread(while having it initialized to that particular wave provider), but I don't get any results.

What's the correct way to handle something like this?
Coordinator
Oct 6, 2014 at 4:06 PM
You should only call init and play on the output device once. After that, just call AddSamples on the bufferedWaveprovider whenever you have new audio to play.
Also, make sure BufferedWaveProvider.PadwithZeroes is set to true which means that the Read method never returns 0, which would cause playback to stop if no audio was queued up.

Mark