在 Silverlight 3 中从 Web 流式传输 WAV 文件
我设法发现 Gilles Khouzam 在 Silverlight 3 中对 WAV 文件的播放实现,虽然这将是战斗的大部分内容,但我仍停留在最后一个细节上:如何从网络上的某个位置提取 wav 文件,然后将其输入到他的 WaveMediaStreamSource 中进行播放?
这是我最接近的:
public MainControl()
{
// Required to initialize variables
InitializeComponent();
PlayButton.Click += PlayButtonClicked;
}
private void PlayButtonClicked(object sender, RoutedEventArgs e)
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(@"soundfile.wav");
request.BeginGetResponse(ReadCallback, request);
}
private void ReadCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
WaveMediaStreamSource wavMss = new WaveMediaStreamSource(response.GetResponseStream());
MediaPlayer.SetSource(wavMss);
}
编辑: 事实证明,问题与 HttpWebRequest 有关。 将代码更改为:
public MainPage()
{
InitializeComponent();
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri(@"http://www.russellmyers.com/somefile.wav", UriKind.Absolute));
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
WaveMediaStreamSource wavMss = new WaveMediaStreamSource(e.Result);
Debug.WriteLine("Setting source...");
Media.SetSource(wavMss);
}
工作正常。 在阅读Shawn Wildermuth 关于差异的文章后,这也是有道理的。 我想让 HttpWebRequest 工作,因为它不会在 UI 线程上完成。
I've managed to discover Gilles Khouzam's playback implementation for WAV files in Silverlight 3 and while that would be the majority of the battle, I'm stuck on a final detail: how do I pull a wav file from the web some place and then feed it into his WaveMediaStreamSource for playback?
Here's the closest I've come:
public MainControl()
{
// Required to initialize variables
InitializeComponent();
PlayButton.Click += PlayButtonClicked;
}
private void PlayButtonClicked(object sender, RoutedEventArgs e)
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(@"soundfile.wav");
request.BeginGetResponse(ReadCallback, request);
}
private void ReadCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
WaveMediaStreamSource wavMss = new WaveMediaStreamSource(response.GetResponseStream());
MediaPlayer.SetSource(wavMss);
}
Edit:
It turns out the problem had to do with HttpWebRequest. Changing the code to:
public MainPage()
{
InitializeComponent();
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri(@"http://www.russellmyers.com/somefile.wav", UriKind.Absolute));
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
WaveMediaStreamSource wavMss = new WaveMediaStreamSource(e.Result);
Debug.WriteLine("Setting source...");
Media.SetSource(wavMss);
}
Works fine. This makes sense too after reading Shawn Wildermuth's article on the differences. I would like to get HttpWebRequest working though because it won't be done on the UI thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论