C# - How to write NAudio WaveStream to a Memory Stream?

I have a program that takes in mp3 data in a byte array. It has to convert that mp3 data into wav format and store it in a byte data. I am trying to use NAudio for this purpose. I am using the following code for this purpose.

Stream inputStream = ...;
Stream outputStream = ...;

using (WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(inputStream)))
using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat))
{
    byte[] bytes = new byte[waveStream.Length];
    waveStream.Read(bytes, 0, waveStream.Length);
    waveFileWriter.WriteData(bytes, 0, bytes.Length);
    waveFileWriter.Flush();
}

When I run the above code, all I receive is 0 in the byte array. But if use WaveFileWriter to write the data directly to a file, the file receives the correct data. Any reasons?

This question and answers originated from www.stackoverflow.com
Question by gibraltar (7/16/2012 7:59:19 AM)

Answer

Give this a try:

using (WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(inputStream))) 
using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat)) 
{ 
    byte[] bytes = new byte[waveStream.Length]; 
    waveStream.Position = 0;
    waveStream.Read(bytes, 0, waveStream.Length); 
    waveFileWriter.WriteData(bytes, 0, bytes.Length); 
    waveFileWriter.Flush(); 
} 
Answer by Chris Gessler

Find More Answers
Related Topics  c#  stream  naudio
Related Questions
  • WaveStream to Byte Array in NAudio?

    How do I convert a WaveStream to a Byte Array using NAudio?
  • Appending to a WaveStream

    I am trying to stream sound using Naudio. The thing I am having trouble with is that I can't write to a WaveStream. I tried making a queue of smaller WaveStream but I don't think this is not the way…
  • Record to a MemoryStream using NAudio

    I'm trying to use NAudio to record from the micrphone and save tthat data to a MemoryStream. There doesn't seem to be an obvious way and everytime I try to read from my BufferedWaveProvider , the re…
  • How to convert WAV stream to MP3 using Naudio?

    I've seen a sample in Naudio documentation that it converts the MP3 stream to WAV stream. My doubt is, does Naudio do it in reverse? That is, can it convert a WAV stream to MP3 stream? If yes, pleas…
  • NAudio stream to Widows Mideaplayer

    I have Windows MediaPlayer embed on a page. I have a audio file(WAV) in MemoryStream I got from a byte[] array. I need to use NAudio to pay that file in memory. how can I point the src of the MediaP…
  • NAudio WaveStream and multiple channels

    Ok, I have WaveStream, I can read WaveFormat to obtain number of channels. So when I read from that WaveStream I'm reading 1st channel or data from all the channels mixed? I wanna do user control to…
  • Stream wrapper to make Stream seekable?

    I have a readonly System.IO.Stream implementation that is not seekable (and it's Position always returns 0). I need to send it to a consumer that does some Seek operations (aka, sets the Position) o…
  • using Naudio to play Stream of Wave

    I want to change the bit rate of wave file. so I searched in the net and I figure out that the wave file contain a header which is 44 bytes length , and the 25,26,27 and 28 byte are used to store…
  • Naudio,how to tell playback is completed

    I am using the NAudio library to write a simple WinForms audio recorder/player. My problem is how can I tell that playback is completed? I need to close the wave stream after that. I knew there i…
  • How do I write RIFF chunk data to a WAV file that was made with NAudio?

    How do I write RIFF chunk data to the end of a WAV file? Yes, that's right, to the end of a WAV file. Why? Because that's the way the old program I'm replacing did it and the integrating programs ar…