如何发挥非缓冲WAV与MediaStreamSource的实施在Silverlight 4?WAV、MediaStreamSource、Silverlight

2023-09-03 12:06:20 作者:总萌大人

背景

我想流在Silverlight 4波形文件使用MediaStreamSource的执行结果here.问题是我要播放的文件,同时它还是缓冲,或者至少给用户提供一些视觉反馈,而它的缓冲。现在我的code样子的:

I'm trying to stream a wave file in Silverlight 4 using MediaStreamSource implementation found here. The problem is I want to play the file while it's still buffering, or at least give user some visual feedback while it's buffering. For now my code looks like that:

private void button1_Click(object sender, RoutedEventArgs e)
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(App.Current.Host.Source, "../test.wav"));
    //request.ContentType = "audio/x-wav";
    request.AllowReadStreamBuffering = false;

    request.BeginGetResponse(new AsyncCallback(RequestCallback), request);
}

private void RequestCallback(IAsyncResult ar)
{
    this.Dispatcher.BeginInvoke(delegate()
    {
        HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);

        WaveMediaStreamSource wavMss = new WaveMediaStreamSource(response.GetResponseStream());

        try
        {
            me.SetSource(wavMss);
        }
        catch (InvalidOperationException)
        {
            // This file is not valid
        }
        me.Play();
    });
}

现在的问题是,设置在 request.AllowReadStreamBuffering = FALSE 该流不支持查找和上面提到的实施将引发异常(请注意,我把一些位置设置逻辑到如果(stream.CanSeek)块):

The problem is that after setting request.AllowReadStreamBuffering = false the stream does not support seeking and the above mentioned implementation throws an exception (keep in mind I've put some of the position setting logic into if (stream.CanSeek) block):

读不支持在主线程时,缓冲被禁止

Read is not supported on the main thread when buffering is disabled

问题

有没有一种方法,而不在Silverlight 4缓冲预先在播放WAV流?

Is there a way to play WAV stream without buffering it in advance in Silverlight 4?

推荐答案

下载流,而你读它是行不通的,因为阅读steram播放将流中移动位置属性,当你将数据写入到流,该位置将不会在正确的位置。这基本上是为什么你无法在流中所追求的。

Downloading a stream while you read it is not going to work because reading the steram for playback will move the Position property within the stream, and when you will write data to the stream, the position will not be at the right position. that's basically why you cannot seek in your stream.

我所做的来解决这个确切的问题是建立一个自定义的内存流(ReadWriteMemoryStream),这将嵌入逻辑读/写,所以我可以下载一端的文件,将其添加到ReadWriteMemoryStream和基础我MediaStreamSource的此ReadWriteMemoryStream而非网络流

What I did to solve that exact problem was to create a custom memory stream (ReadWriteMemoryStream) which would embed the logic for reading / writing so I could download the file on one end, add it to the ReadWriteMemoryStream and base my MediaStreamSource on this ReadWriteMemoryStream instead of the network stream.

您可以在这里找到我实​​现这个ReadWriteMemoryStream的:

You can find my implementation of this ReadWriteMemoryStream here :

的https://github.com/salfab/open-syno/blob/master/OpenSyno/OpenSyno/ReadWriteMemoryStream.cs

您也可以为了看看它的使用,如果你不能确定它检查项目的其余部分。

you can also check the rest of the project in order to have a look at its usage if you are unsure about it.

希望这有助于。