0

我正在创建一个播放无尽音频流的应用程序。我可以查询一个单独的 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("<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);
            }
        }  
    }

每当我尝试从 HttpWebRequest 读取响应时,都会引发 Web 异常。这是正确的方法吗?有人对我如何解决这个问题有建议吗?

4

2 回答 2

0

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

看看我对这个帖子的回复: AudioPlayerAgent, timer and webservice

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

于 2012-05-15T23:21:15.797 回答
0

您没有关闭HttpWebResponse,这是必需的。此外,XDocument.Load()这需要 a的重载,Stream因此您根本不需要使用 a 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>();
                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);
        }
    }  
}
于 2012-05-06T22:12:56.020 回答