Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert byte array to wav file

Tags:

c#

bytearray

wav

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();
}
like image 214
Eyla Avatar asked Apr 19 '10 05:04

Eyla


2 Answers

Try this:

System.IO.File.WriteAllBytes("yourfilepath.wav", bytes);
like image 153
Jay Riggs Avatar answered Sep 16 '22 11:09

Jay Riggs


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 :-)

like image 22
Paul Kohler Avatar answered Sep 18 '22 11:09

Paul Kohler