2

我已经设法在 Silverlight 3 中发现Gilles Khouzam对 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 线程上完成。

4

0 回答 0