我正在创建一个播放无尽音频流的应用。有一个单独的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异常。这是正确的方法吗?有没有人有关于如何解决这个问题的建议?
答案 0 :(得分:0)
你没有关闭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);
}
}
}
答案 1 :(得分:0)
我认为您应该将OnUserAction()中的NotifyComplete()函数移动到您的HttpWebRequest响应中。也许这篇文章可以为您提供一些帮助:)
答案 2 :(得分:0)
这与AudioPlayer开始播放音乐后超出范围有关。 AudioPlayer仅存活一段时间,并在调用NotifyComplete
看看我对这篇文章的回复: AudioPlayerAgent, timer and webservice
更多信息:
调用NotifyComplete
后,后台音频线程将“暂停”。返回的方式是用户更改播放(OnUserAction)或歌曲结束时(OnPlayStateChanged)。如果您将继续播放,请在OnPlayStateChanged方法中获取新信息。