来自AudioPlayerAgent的HttpWebRequest

我正在创建一个播放无尽音频流的应用程序。 有一个单独的Web服务,我可以查询以获取当前播放曲目的标题和艺术家。 我想要做的是每20秒查询一次该服务,然后相应地设置曲目标题/艺术家。 目前我正在使用背景AudioPlayerAgent,以便可以在我的应用程序之外播放流。 这是我到目前为止的代码:

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(""); // 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 artist = (from t in trackXml.Descendants("channel") select t.Element("artist").Value).First(); 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); } } } 

每当我尝试从HttpWebRequest读取响应时,就会抛出Webexception。 这是正确的方法吗? 有没有人有关于如何解决这个问题的建议?

你没有关闭HttpWebResponse ,这是必要的。 另外, XDocument.Load()的重载会占用Stream因此您根本不需要使用StreamReader

编辑:对不起,我最后忽略了Close()调用。 但另一条评论仍然适用。

如果它没有解决问题,至少它会让你的代码看起来更干净:

 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 artist = (from t in trackXml.Descendants("channel") select t.Element("artist").Value).First(); 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); } } } 

我认为你应该将OnUserAction()中的NotifyComplete()函数移动到你的HttpWebRequest响应中。也许这篇文章可以给你一些帮助:)

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

这与AudioPlayer在开始播放音乐后超出范围有关。 AudioPlayer仅存活一段时间,并在调用NotifyComplete后终止

看看我对这篇文章的回复: AudioPlayerAgent,计时器和webservice

更多信息:调用NotifyComplete后,后台音频线程将“暂停”。 返回的方式是用户更改播放(OnUserAction)或歌曲结束时(OnPlayStateChanged)。 如果您将继续播放,请在OnPlayStateChanged方法中获取新信息。