背景
我正在尝试使用此处找到的 MediaStreamSource 实现在 Silverlight 4 中流式传输波形文件。问题是我想在文件仍在缓冲时播放文件,或者至少在缓冲时给用户一些视觉反馈。现在我的代码看起来像这样:
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
流后不支持搜索并且上述实现抛出异常(请记住,我已将一些位置设置逻辑放入if (stream.CanSeek)
块中):
禁用缓冲时,主线程不支持读取
问题
有没有办法在 Silverlight 4 中播放 WAV 流而不预先缓冲它?