HttpWebRequest by AudioPlayerAgent

I am creating an application that plays an endless stream of audio. There is a separate web service that I can request to get the title and artist of the current track being played. What I want to do is request this service every 20 seconds and then set the name of the track / artist accordingly. I am currently using background AudioPlayerAgent so that the stream can be played outside of my application. Here is the code that I still have:

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); } } } 

Anytime I try to read a response from HttpWebRequest, a web exception is thrown. Is it correct? Anyone have any suggestions on how I can fix this?

0
source share
3 answers

You do not close HttpWebResponse , which is a must. In addition, there is an overload of XDocument.Load() that accepts Stream , so you don't need to use StreamReader .

EDIT: Sorry, I did not pay attention to the Close() call at the end. But another comment still applies.

If this does not solve the problem, at least it will make your code 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); } } } 
0
source

I think you just need to move the NotifyComplete () function to OnUserAction () to your HttpWebRequest answer. Perhaps this article may help you :)

http://tmango.com/?p=952

0
source

This is due to the fact that AudioPlayer goes out of scope after the start of music playback. AudioPlayer only lives for a fraction of the time, and it stopped after calling NotifyComplete

Take a look at my answer to this post: AudioPlayerAgent, timer and web service

Additional Information: The background audio stream will “pause” after calling NotifyComplete . The return path is when the user changes the game (OnUserAction) or when the song ends (OnPlayStateChanged). If you continue to play, get new information in the OnPlayStateChanged method.

0
source

Source: https://habr.com/ru/post/971212/


All Articles