编程语言
首页 > 编程语言> > C#-AudioPlayerAgent,计时器和Web服务

C#-AudioPlayerAgent,计时器和Web服务

作者:互联网

我的应用程序读取了一个广播.

从网络服务收集播放的音乐的元数据,该服务会返回一个Json(因此我不必解码流).我每20秒使用一个计时器调用一次Web服务,这在我的应用程序中有效,但在AudioPlayer.cs中不起作用

    //Timer
    private DispatcherTimer timer;


    /// <remarks>
    /// AudioPlayer instances can share the same process. 
    /// Static fields can be used to share state between AudioPlayer instances
    /// or to communicate with the Audio Streaming agent.
    /// </remarks>
    public AudioPlayer()
    {
        if (!_classInitialized)
        {
            _classInitialized = true;
            // Subscribe to the managed exception handler
            Deployment.Current.Dispatcher.BeginInvoke(delegate
            {
                Application.Current.UnhandledException += AudioPlayer_UnhandledException;

            });
        }

        //I activate the timer
        timer = new DispatcherTimer
        {
            Interval = TimeSpan.FromSeconds(20) // <------- Error here UnauthorizedAccessException was unhandled. Invalid cross-thread access.
        };
        timer.Tick += timer_Tick;
        timer.Start();
    }


    private void timer_Tick(object sender, EventArgs e)
    {
        HttpWebRequest request = WebRequest.Create("http://127.0.0.81:8003/getmeta") as HttpWebRequest;
        request.BeginGetResponse(new AsyncCallback(AsyncBack), request);
    }

    private void AsyncBack(IAsyncResult ias)
    {
            HttpWebRequest req = (HttpWebRequest)ias.AsyncState;

            try
            {
                using (HttpWebResponse res = req.EndGetResponse(ias) as HttpWebResponse)
                {
                    StreamReader stream = new StreamReader(res.GetResponseStream());
                    String jsonToParse = stream.ReadToEnd();
                    JObject jObject = JObject.Parse(jsonToParse);

                    AudioTrack track = BackgroundAudioPlayer.Instance.Track;

                    track.BeginEdit();
                    track.Title = (string) jObject["title"];
                    track.Artist = (string) jObject["artist"];
                    track.Album = (string) jObject["album"];
                    track.EndEdit();


                    res.Close();
                }
            }
            catch (WebException e)
            {
                timer.Stop();
            }
    }

谢谢你的帮助

解决方法:

AudioPlayer类非常独特.它只生存一小段时间.在使用BackgroundAudioPlayer的应用程序中,您对AudioPlayer类的实现将一直存活下来以完成更改播放状态的任务.因此,当用户开始播放某些内容时,将创建AudioPlayer类的实例来完成开始播放的任务.在OnUserAction或OnPlayStateChanged中调用NotifyComplete()后,AudioPlayer的实例就会消失.

与AudioPlayer关联的后台线程仍将处于活动状态,并且您可以在该线程中保留其他对象,但是AudioPlayer将终止.创建的默认AudioPlayer使用_classInitialized字段对此进行提示.这是静态的,因为AudioPlayer将创建多次,但是我们只希望订阅该事件一次.

我建议两件事之一.第一种是仅在需要前进到下一首歌曲时才获得json响应.在响应返回之前,您不会调用NotifyComplete().
这是一些伪代码:

override OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param)
{
    GetJsonResponse();
}
private void GetJsonResponce()
{
    // async call to your service
    // When async completes:
    track.BeginEdit();
    track.Title = (string) jObject["title"];
    track.Artist = (string) jObject["artist"];
    track.Album = (string) jObject["album"];
    track.EndEdit();
    NotifyComplete();
}

第二个是要有一个在后台执行此操作的类.该类将具有静态属性,以获取线程中仍存在的实例.然后,您的AudioPlayer将从该对象获取所需的信息

public class Songs
{
    static Songs _instance;
    public static Songs Instance
    { 
        get { return _instance ?? new Songs(); }
    }
    // Do you timer stuff here

    // Allow the ability to access the timer stuff also.
}
// in AudioPlayer
override OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param)
{
    Songs.Instance.GetStuff
    NotifyComplete();
}

标签:timer,background-process,windows-phone-7,windows-phone-7-1,c
来源: https://codeday.me/bug/20191101/1984649.html