views:

228

answers:

1

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?

A: 

hello All I have changed this line " HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(App.Current.Host.Source, "../test.wav"));" to
"HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://localhost:1502/PlayAudio.Web/PlayFile/gg.wav", UriKind.RelativeOrAbsolute));" But I have get error "DrmExaception was unhandled by user code". plase help to solve this error

saif
Sorry but it's to little info + you would be better off creating a new question (as it's not connected with mine directly) and using tags for your code (so it will be more readable). "DrmExaception"? you mean exception right? :)
kyrisu