15

I'm trying to play a wav sound that stored in byte array called bytes. I know that I should convert the byte array to wav file and save it in my local drive then called the saved file but I was not able to convert the byte array to wav file.

please help me to give sample code to convert byte arrary of wav sound to wav file.

here is my code:

protected void Button1_Click(object sender, EventArgs e)
{
    byte[] bytes = GetbyteArray();

   //missing code to convert the byte array to wav file

    .....................

    System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer(myfile);
    myPlayer.Stream = new MemoryStream();
    myPlayer.Play();
}
3
  • Out of curiosity: How did you get the sound in the byte array in the first place? Commented Apr 19, 2010 at 6:16
  • This is apart of a dictionary project that download sound of each word in my dictionary from merriam-webster.com. and here sample of wav file that downloaded and saved in the byte array:media.merriam-webster.com/soundc11/g/good0001. Commented Apr 19, 2010 at 16:29
  • Hi Eyla, could you please provide the code for reading wave format file into a byte array in android.Would be very thankful. Commented Oct 14, 2012 at 11:37

3 Answers 3

13

Try this:

System.IO.File.WriteAllBytes("yourfilepath.wav", bytes);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your help. your code work for me and convert the byte array to wav file and saved it in given path but the problem that when I use: System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer(myfile); to play it will give me error which is :The wave header is corrupt. but when I use windows media player to play the file it will work. any advice to save this problem?
I just tried the URL you supplied and it worked: new System.Media.SoundPlayer(@"http://media.merriam-webster.com/soundc11/g/good0001").Play(); - perhaps the problem is the download code?
The problem here is not corrupted data. The OP only has raw audio bytes. He will need to write the WAV header to file before the byte array and compensate for big / little endian formats. I'm looking for some clean code to do this as well.
You'll need the header, which will include the file size, so you'll be adding it later: docs.fileformat.com/audio/wav
7

You can use something like File.WriteAllBytes(path, data) or...

...Alternatively if you don't want to write the file you could convert the byte array to a stream and then play that...

var bytes = File.ReadAllBytes(@"C:\WINDOWS\Media\ding.wav"); // as sample

using (Stream s = new MemoryStream(bytes))
{
    // http://msdn.microsoft.com/en-us/library/ms143770%28v=VS.100%29.aspx
    System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer(s);
    myPlayer.Play();
}

PK :-)

2 Comments

Apologies, I left the myPlayer.Stream = new MemoryStream() code in from the sample which would have cancelled out the constructor stream...
Thank you for your help. your code works too and I voted up for you too.
3

Using NAudio and you can try something like:

//var wavReader = new WaveFileReader(yourWavFilePath);
//byte[] buffer = new byte[2 * wav1Reader.WaveFormat.SampleRate * wav1Reader.WaveFormat.Channels];
byte[] buffer = YourWaveSoundByteArray;

using ( WaveFileWriter writer = new WaveFileWriter(YourOutputFilePath, new WaveFormat( AssignWaveFormatYouWant /*wavReader.WaveFormat.SampleRate, 16, 2/*how many channel*/))
    )
{
    //int bytesRead;
    //while ((bytesRead = wavReader.Read(buffer, 0, buffer.Length)) > 0)
    //{
        writer.Write(buffer, 0,  buffer.Length/*bytesRead*/);
    //}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.