C# 将ThreadPoolTimer与背景音频UWP一起使用
我正在使用BackgroundAudioTask制作一个UWP应用程序。我的应用程序运行得很好。现在,我想在文本块中添加播放音频的当前位置 在执行音频任务之前,我正在使用此方法:C# 将ThreadPoolTimer与背景音频UWP一起使用,c#,win-universal-app,uwp,dispatchertimer,C#,Win Universal App,Uwp,Dispatchertimer,我正在使用BackgroundAudioTask制作一个UWP应用程序。我的应用程序运行得很好。现在,我想在文本块中添加播放音频的当前位置 在执行音频任务之前,我正在使用此方法: private TimeSpan TotalTime; private DispatcherTimer timerRadioTime; private void radioPlayer_MediaOpened(object sender, RoutedEventArgs e)
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;
}
}
}
当我执行后台任务时,它给了我一个异常。在研究之后,我看到了使用ThreadPoolTimer而不是Dispatcher的建议
我尝试编写此代码(遵循此解决方案:)
这显然是行不通的。应用程序进入该方法而不更新我的UI。我真的不明白计时器应该是什么。有没有关于如何运行的想法?解决方案:
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;
//仅用于显示时间
私有无效CurrentPlayer_MediaOpen(MediaPlayer发送方,对象参数)
{
timer=ThreadPoolTimer.CreatePeriodicTimer(_clockTimer_Tick,TimeSpan.FromSeconds(1));
}
专用异步无效\u时钟计时器\u滴答(线程池计时器)
{
var dispatcher=Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.dispatcher;
等待dispatcher.RunAsync(
CoreDispatcherPriority.Normal,()=>
{
//你的UI更新代码在这里!
如果(CurrentPlayer.NaturalDuration.TotalSeconds<0)
{
TimeSpan currentPos=当前播放器位置;
var currentTime=string.Format(“{0:00}:{1:00}”,(currentPos.Hours*60)+currentPos.Minutes,currentPos.Seconds);
CurrentPosition.Text=currentTime;
}
});
}
“它给了我一个异常”不是一个合适的问题描述。请使用“ThreadPoolTimer.CreatePeriodicTimer(_-clockTimer_-Tick,TimeSpan.FromMillics(1000));”要创建计时器,你能帮我解决这个问题吗?
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;
}
});
}