本文介绍了使用ThreadPoolTimer带背景音乐的UWP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用BackgroundAudioTask一个UWP应用程序。我的应用程序工作得很好。现在我想在一个TextBlock音频的当前位置起到添加

I am making a UWP app using the BackgroundAudioTask. My app is working very well. Now I want to add in a TextBlock the Current position of the Audio played.

我在做执行音频任务之前,此方法:

I was doing this method before implementing the audio Task:

private TimeSpan TotalTime;
        private DispatcherTimer timerRadioTime;

        private void radioPlayer_MediaOpened(object sender, RoutedEventArgs e)
        {
            TotalTime = radioPlayer.NaturalDuration.TimeSpan;

            // Create a timer that will update the counters and the time slider
            timerRadioTime = new DispatcherTimer();
            timerRadioTime.Interval = TimeSpan.FromSeconds(1);
            timerRadioTime.Tick += TimerRadioTime_Tick;
            timerRadioTime.Start();
        }

        private void TimerRadioTime_Tick(object sender, object e)
        {
            //Check if the audio finished calculate it's total time
            if (radioPlayer.NaturalDuration.TimeSpan.TotalSeconds > 0)
            {
                if (TotalTime.TotalSeconds > 0)
                {
                    // Updating timer
                    TimeSpan currentPos = radioPlayer.Position;
                    var currentTime = string.Format("{0:00}:{1:00}", (currentPos.Hours * 60) + currentPos.Minutes, currentPos.Seconds);
                    radioTimerBlock.Text = currentTime;
                }
            }
        }

当我实现了后台任务它给了我一个例外。 。研究之后,我看到了使用dispatcherTimer的ThreadPoolTimer而不是建议

When I implemented the Background Task it gave me an Exception. After researching I saw a suggestion of using ThreadPoolTimer instead of dispatcherTimer.

我试着写代码(以下这个解决方案:的时钟程序)

I tried writing this code (following this solution: Clock program employing ThreadPoolTimer C# uwp)

ThreadPoolTimer timer;

        // for displaying time only
        private void CurrentPlayer_MediaOpened(MediaPlayer sender, object args)
        {
            _clockTimer_Tick(timer);
        }


        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;
                 }

             });
        }

这显然是行不通的。该应用程序进入,而不更新我的UI方法。我真的不明白是什么定时应该的。 ?如何使其运行任何想法

This is obviously not working. The app enters the method without updating my UI. I really don't understand what timer should be. Any Idea on how to make it run?

推荐答案

解决方案:

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;
                     }

                 });
            }

这篇关于使用ThreadPoolTimer带背景音乐的UWP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 14:01