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.
我想播放一个wav声音,存储在字节数组中。我知道我应该将字节数组转换为wav文件,并将其保存在本地驱动器中,然后调用已保存的文件,但我无法将字节数组转换为wav文件。
please help me to give sample code to convert byte arrary of wav sound to wav file.
请帮助我提供示例代码,将wav声音的字节arrary转换为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();
}
3 个解决方案
#1
10
Try this:
试试这个:
System.IO.File.WriteAllBytes("yourfilepath.wav", bytes);
#2
7
You can use something like File.WriteAllBytes(path, data)
or...
你可以使用文件之类的东西。WriteAllBytes(路径、数据)或…
...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:-)
#3
1
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*/);
//}
}
#1
10
Try this:
试试这个:
System.IO.File.WriteAllBytes("yourfilepath.wav", bytes);
#2
7
You can use something like File.WriteAllBytes(path, data)
or...
你可以使用文件之类的东西。WriteAllBytes(路径、数据)或…
...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:-)
#3
1
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*/);
//}
}