HttpWebRequest from AudioPlayerAgent

本文关键字:AudioPlayerAgent from HttpWebRequest | 更新日期: 2023-09-27 18:33:17

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

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 异常。这是正确的方法吗?有人对我如何解决这个问题有建议吗?

HttpWebRequest from AudioPlayerAgent

你没有关闭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>();
                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);
        }
    }  
}

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

看看我对这篇文章的回复:AudioPlayerAgent, timer and Web Service

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

相关文章:
  • 没有找到相关文章