我有一个.wav文件,我将此字节格式写入XML。我想以自己的形式播放这首歌,但不确定自己是否正确,因此无法正常工作。 Str是我文件的字节格式。

byte[] soundBytes = Convert.FromBase64String(str);
MemoryStream ms = new MemoryStream(soundBytes, 0, soundBytes.Length);
ms.Write(soundBytes, 0, soundBytes.Length);
SoundPlayer ses = new SoundPlayer(ms);
ses.Play();

最佳答案

我认为问题在于您正在使用缓冲区初始化MemoryStream,然后将相同的缓冲区写入流中。因此,该流从给定的数据缓冲区开始,然后使用相同的缓冲区覆盖它,但是在此过程中,您还将流中的当前位置更改到最后。

byte[] soundBytes = Convert.FromBase64String(str);
MemoryStream ms = new MemoryStream(soundBytes, 0, soundBytes.Length);
// ms.Position is 0, the beginning of the stream
ms.Write(soundBytes, 0, soundBytes.Length);
// ms.Position is soundBytes.Length, the end of the stream
SoundPlayer ses = new SoundPlayer(ms);
// ses tries to play from a stream with no more bytes to consume
ses.Play();

删除对ms.Write()的调用,看看它是否有效。

10-08 13:19