2012-07-31 21 views
0

我有一个.wav文件,我将这个字节格式写入XML。我想在我的表格上播放这首歌曲,但我不确定我是否正确,并且不起作用。 Str是我的文件的字节形式。XML声音的字节形式

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(); 

回答

1

我认为这个问题是你是一个缓冲初始化你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()的呼叫,看看它是否有效。