HttpWebRequest from AudioPlayerAgent

2020-03-31 07:07发布

问题:

I'm creating an app that plays an endless audio stream. There is a separate web service that I can query to get the title and artist of the currently playing track. What I want to do is query that service every 20 seconds and then set the track title/artist accordingly. Currently I'm using a background AudioPlayerAgent so that the stream can be played outside of my application. Here is the code I have so far:

public AudioPlayer()
    {
        if (!_classInitialized)
        {
            _classInitialized = true;
            // Subscribe to the managed exception handler
            Deployment.Current.Dispatcher.BeginInvoke(delegate
            {
                Application.Current.UnhandledException += AudioPlayer_UnhandledException;

            });
            trackTimer = new Timer(TrackTimerTick, null, 1000, 5000);
        }
    }

    public void TrackTimerTick(object state) {             
            // Create a HttpWebrequest object to the desired URL.
            HttpWebRequest trackRequest = (HttpWebRequest)HttpWebRequest.Create("<stream url>");
            // Start the asynchronous request.
            IAsyncResult result = (IAsyncResult)trackRequest.BeginGetResponse(new AsyncCallback(TrackCallback), trackRequest);
    }

    public void TrackCallback(IAsyncResult result) {
        if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing && result != null) {
            try {
                // State of request is asynchronous.
                HttpWebRequest trackRequest = (HttpWebRequest)result.AsyncState;
                HttpWebResponse trackResponse = (HttpWebResponse)trackRequest.EndGetResponse(result);
                using (StreamReader httpwebStreamReader = new StreamReader(trackResponse.GetResponseStream())) {
                    string results = httpwebStreamReader.ReadToEnd();
                    StringReader str = new StringReader(results);
                    XDocument trackXml = XDocument.Load(str);

                    string title = (from t in trackXml.Descendants("channel") select t.Element("title").Value).First<string>();
                    string artist = (from t in trackXml.Descendants("channel") select t.Element("artist").Value).First<string>();
                    if (BackgroundAudioPlayer.Instance.Track != null) {
                        AudioTrack track = BackgroundAudioPlayer.Instance.Track;
                        track.BeginEdit();
                        track.Title = title;
                        track.Artist = artist;
                        track.EndEdit();
                    }

                }
                trackResponse.Close();
                NotifyComplete();
            } catch (WebException e) {
                Debug.WriteLine(e);
                Debug.WriteLine(e.Response);
            } catch (Exception e) {
                Debug.WriteLine(e);
            }
        }  
    }

A web exception is thrown anytime that I try to read the response from the HttpWebRequest. Is this the right way to do this? Does anyone have suggestions as to how I can fix this?

回答1:

You're not closing the HttpWebResponse, which is a necessity. Also, there's an overload of XDocument.Load() that takes a Stream so you don't need to use a StreamReader at all.

EDIT: Sorry, I overlooked the Close() call at the end. But the other comment still applies.

If it doesn't solve the problem, atleast it makes your code look cleaner:

public void TrackCallback(IAsyncResult result) {
    if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing && result != null) {
        try {
            // State of request is asynchronous.
            HttpWebRequest trackRequest = (HttpWebRequest)result.AsyncState;
            using (HttpWebResponse trackResponse = (HttpWebResponse)trackRequest.EndGetResponse(result)){
                XDocument trackXml = XDocument.Load(trackResponse.GetResponseStream());

                string title = (from t in trackXml.Descendants("channel") select t.Element("title").Value).First<string>();
                string artist = (from t in trackXml.Descendants("channel") select t.Element("artist").Value).First<string>();
                if (BackgroundAudioPlayer.Instance.Track != null) {
                    AudioTrack track = BackgroundAudioPlayer.Instance.Track;
                    track.BeginEdit();
                    track.Title = title;
                    track.Artist = artist;
                    track.EndEdit();
                }
            }
            }
            NotifyComplete();
        } catch (WebException e) {
            Debug.WriteLine(e);
            Debug.WriteLine(e.Response);
        } catch (Exception e) {
            Debug.WriteLine(e);
        }
    }  
}


回答2:

This has to do with the AudioPlayer going out of scope after it begins playing the music. The AudioPlayer only lives for a fraction of time and it terminated after the call to NotifyComplete

Take a look at my reply to this post: AudioPlayerAgent, timer and webservice

More info: The background audio thread will "suspend" after NotifyComplete is called. The way back in is when the user changes play (OnUserAction), or when the song ends (OnPlayStateChanged). If you will continue to play, get the new info within the OnPlayStateChanged method.