This project has moved and is read-only. For the latest updates, please go here.

write wav byte data to file and play from that

Jan 2, 2011 at 8:50 AM

I am trying to load wav file to stream then i want to write that byte data to file and then i want to read that file and play it,

but when before writing to file i need to take to buffer, when i am trying to load it to buffer my computer become hang, my code is given below:


WaveStream wavReader = new WaveFileReader (fileName);

WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(wavReader);

 WaveStream blockAlignedStream = new BlockAlignReductionStream (pcmStream);

WaveChannel32 waveChannel = new WaveChannel32 (blockAlignedStream);


WaveFormat format = new WaveFormat (waveChannel.WaveFormat.SampleRate, waveChannel.WaveFormat.BitsPerSample, waveChannel.WaveFormat.Channels);


byte[] buffer1 = new byte [waveChannel.WaveFormat.SampleRate];

BufferedWaveProvider inputProvider = new BufferedWaveProvider (waveChannel.WaveFormat);


int bytesRead;

for (int i = 0; i <waveChannel.Length; i++) {

   bytesRead = waveChannel.Read(buffer1, 0, waveChannel.WaveFormat.SampleRate);

    inputProvider.AddSamples(buffer1, 0, bytesRead);




///end of code

my logic is if I can play the inputProvider which is created from bytesRead,buffer1, and if I will be able to write the value of bytesRead,buffer1 to a file then  I

will be able to read these values from file and play it by inputProvider.

in this example I tested by  files which length is 1-2sec. my computer becomes hang when it is in for loop

I also increase the value of


in the

BufferedWaveProvider class

Jan 2, 2011 at 12:31 PM

your for loop is going round far too many times.

try a while (waveChannel.Position < waveChannel.Length) instead


Jan 2, 2011 at 4:05 PM


Thank u very much. its working now,

Jan 2, 2011 at 7:03 PM
Edited Jan 3, 2011 at 9:39 AM

Now I have another problem. I can write buffer data to file. code as follow:


WaveFormat format = new WaveFormat(waveChannel.WaveFormat.SampleRate, waveChannel.WaveFormat.BitsPerSample, waveChannel.WaveFormat.Channels);
            BufferedWaveProvider inputProvider = new BufferedWaveProvider(waveChannel.WaveFormat);
            byte[] buffer1 = new byte[waveChannel.WaveFormat.SampleRate];
            int bytesRead;
            FileStream fs = new FileStream("c1.txt", FileMode.CreateNew);
            // Create the writer for data.
            BinaryWriter w = new BinaryWriter(fs);
            // Write data
            while (waveChannel.Position < waveChannel.Length)
                bytesRead = waveChannel.Read(buffer1, 0, waveChannel.WaveFormat.SampleRate);
                for (int j = 0; j < 4800; j++)
                inputProvider.AddSamples(buffer1, 0, bytesRead);

  But I can't play that audio from reading byte data from that file. I can't understand how to start. Please Help


Jan 4, 2011 at 11:38 AM

your best bet is to use the WaveFileWriter class to make a WAV file