HttpWebRequest的从AudioPlayerAgent [英] HttpWebRequest from AudioPlayerAgent

查看:179
本文介绍了HttpWebRequest的从AudioPlayerAgent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建中发挥着无尽的音频流的应用程序。还有,我可以查询来获取当前播放曲目的标题和艺术家独立的Web服务。我想要做的就是查询服务,每20秒钟,然后设置相应的曲目名称/艺术家。目前我使用的背景下AudioPlayerAgent,使流,可以我的应用程序之外播放。这里是我的代码至今:

I'm creating an app that plays an endless audio stream. There is a separate web service that I can query to get the title and artist of the currently playing track. What I want to do is query that service every 20 seconds and then set the track title/artist accordingly. Currently I'm using a background AudioPlayerAgent so that the stream can be played outside of my application. Here is the code I have so far:

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的响应。这是做的正确方法?有没有人有任何建议,我怎么能解决这个问题?

A web exception is thrown anytime that I try to read the response from the HttpWebRequest. Is this the right way to do this? Does anyone have suggestions as to how I can fix this?

推荐答案

这已与AudioPlayer后,走出去的范围做开始播放音乐。该AudioPlayer只住了时间的一小部分,它调用 NotifyComplete

This has to do with the AudioPlayer going out of scope after it begins playing the music. The AudioPlayer only lives for a fraction of time and it terminated after the call to NotifyComplete

后终止看看我的回复这个帖子:
AudioPlayerAgent,定时器和WebService

Take a look at my reply to this post: AudioPlayerAgent, timer and webservice

更多信息:
NotifyComplete 背景声频线程将暂停之称。回的方式是当用户改变播放(OnUserAction)时,或者当歌曲结束(OnPlayStateChanged)。如果你将继续发挥,拿到OnPlayStateChanged方法中的新的信息。

More info: The background audio thread will "suspend" after NotifyComplete is called. The way back in is when the user changes play (OnUserAction), or when the song ends (OnPlayStateChanged). If you will continue to play, get the new info within the OnPlayStateChanged method.

这篇关于HttpWebRequest的从AudioPlayerAgent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆