在 Silverlight 3 中从 Web 流式传输 WAV 文件

发布于 2024-07-21 22:20:27 字数 1981 浏览 6 评论 0原文

我设法发现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文