Stream a WAV File From The Web In Silverlight 3

2019-07-17 09:24发布

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.

0条回答
登录 后发表回答