Using ThreadPoolTimer with Background Audio UWP

巧了我就是萌 提交于 2019-12-04 20:22:36

Solution:

ThreadPoolTimer timer;

            // for displaying time only
            private void CurrentPlayer_MediaOpened(MediaPlayer sender, object args)
            {
                timer = ThreadPoolTimer.CreatePeriodicTimer(_clockTimer_Tick, TimeSpan.FromSeconds(1));
            }


            private async void _clockTimer_Tick(ThreadPoolTimer timer)
            {
                var dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;
                await dispatcher.RunAsync(
                 CoreDispatcherPriority.Normal, () =>
                 {

                     // Your UI update code goes here!
                     if (CurrentPlayer.NaturalDuration.TotalSeconds < 0)
                     {
                         TimeSpan currentPos = CurrentPlayer.Position;
                         var currentTime = string.Format("{0:00}:{1:00}", (currentPos.Hours * 60) + currentPos.Minutes, currentPos.Seconds);
                         CurrentPosition.Text = currentTime;
                     }

                 });
            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!