简体   繁体   English

MediaElement随机停止播放MemoryStream的音频

[英]MediaElement randomly stops playing audio from MemoryStream

In my Silverlight application, I read audio data from a File with a ZipInputStream, then store it in a MemoryStream. 在我的Silverlight应用程序中,我使用ZipInputStream从文件中读取音频数据,然后将其存储在MemoryStream中。 Here's the code I'm using: 这是我正在使用的代码:

byte[] buf = new byte[1024];
MemoryStream memoryStream = new MemoryStream();
int len;
while ((len = zipInputStream.Read(buffer, 0, buffer.Length)) > 0)
{
    memoryStream.Write(buf, 0, len);
}

// Reset the position for reading.
memoryStream.Position = 0;
// Check how large the byte[] is.
textBox.Text = memoryStream.ToArray().Length.ToString();    

MediaElement me = new MediaElement();
me.setSource(memoryStream);
me.Play();

This code partly works; 该代码部分有效; the song from the input file starts playing. 输入文件中的歌曲开始播放。 In addition, the byte[] always has the same length for the same song. 另外,对于同一首歌曲,byte []始终具有相同的长度。 I take this to mean that the song is being completely read each time. 我的意思是每次都会完整读取这首歌。

However, my problem is that the audio randomly stops playing at a different point each run through. 但是,我的问题是音频会在每次运行的不同点随机停止播放。 The song has not yet fully played, either. 这首歌也尚未完全播放。 I'm not exactly sure why this happens. 我不确定为什么会这样。

If anyone knows, I'd like to know why this is happening. 如果有人知道,我想知道为什么会这样。 I'd also like to know if there's something wrong with my code, or if there's a different way I should go about storing the audio (that doesn't involve storing a file on the user's computer). 我还想知道我的代码是否有问题,或者我是否应该以其他方式存储音频(这不涉及在用户计算机上存储文件)。

I was finally able to find a solution. 我终于能够找到解决方案。 By making the MediaElement and MemoryStream global variables, the song played through completely each time. 通过使MediaElement和MemoryStream成为全局变量,歌曲每次都会完全播放。 I'm still not 100% sure what caused this error, although my best guess is that the problem was caused by the garbage collector deleting the stream. 我仍然不是100%知道是什么导致了此错误,尽管我最好的猜测是问题是由垃圾收集器删除了流引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM