[英]Convert byte array to wav file
I'm trying to play a wav sound that stored in byte array called bytes. 我正在尝试播放存储在称为字节的字节数组中的wav声音。 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. 我知道我应该将字节数组转换为wav文件并将其保存在我的本地驱动器中然后调用保存的文件,但我无法将字节数组转换为wav文件。
please help me to give sample code to convert byte arrary of wav sound to wav file. 请帮我提供示例代码,将wav声音的字节转换为wav文件。
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();
}
尝试这个:
System.IO.File.WriteAllBytes("yourfilepath.wav", bytes);
You can use something like File.WriteAllBytes(path, data)
or... 您可以使用类似File.WriteAllBytes(path, data)
或...
...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 :-) PK :-)
Using NAudio and you can try something like: 使用NAudio ,您可以尝试以下方式:
//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*/);
//}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.