来自 AudioPlayerAgent 的 HttpWebRequest [英] HttpWebRequest from AudioPlayerAgent

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

问题描述

我正在创建一个可以播放无限音频流的应用.我可以查询一个单独的 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 读取响应时,都会引发 Web 异常.这是正确的方法吗?有人对我如何解决这个问题有建议吗?

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、计时器和网络服务

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.

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

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