How to play non buffered .wav with MediaStreamSource implementation in Silverlight 4?

Posted by kyrisu on Stack Overflow See other posts from Stack Overflow or by kyrisu
Published on 2010-05-30T18:45:33Z Indexed on 2010/05/30 18:52 UTC
Read the original article Hit count: 907

Background

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

The problem is that after settings request.AllowREadStreamBuffer to 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

Question

Is there a way to play wav stream without buffering it in advance?

© Stack Overflow or respective owner

Related posts about c#

Related posts about silverlight-4.0