2

您好,作为在后台使用 播放轻松音乐的应用程序的一部分BackgroundMediaPlayer,我想实现一种在设定时间后关闭音乐的方法..

我的想法是让TimerPicker用户选择音乐何时停止,然后在 appxmanifest 中的声明下将 Timer 作为支持的任务类型添加到我的 MusicBackgroundTask 中。

然后以某种方式正确地使用ThreadPoolTimer我的 MusicBackgroundTask Windows 运行时组件中的 a 在它滴答作响时停止音乐,但我真的无法在网上找到任何解释如何在后台任务中使计时器工作的地方。

因此,如果有人有一个很好的链接来解释或改进一些工作代码,那将是一个很大的帮助,非常感谢你..

4

1 回答 1

1

Here is an MSDN sample that shows an IBackgroundTask implementation using a ThreadPoolTimer (see SampleBackgroundTask.cs): http://code.msdn.microsoft.com/windowsapps/Background-Task-Sample-9209ade9/sourcecode?fileId=43572&pathId=498327315

One potential reason for this not working is if you did not keep a reference to the object returned from IBackgroundTaskInstance.GetDeferral() which would cause your background task to complete prematurely after IBackgroundTask.Run returns. The sample linked above does this and I believe this is also required for the BackgroundMediaPlayer Audio task to continue playing music so I would be surprised if you didn't already have this.

Finally, the "Timer" task type in your MusicBackgroundTask declaration in the appxmanifest file will have no impact for your usage of ThreadPoolTimer. The "Timer" task type is to allow an IBackgroundTask entrypoint to be triggered by TimeTrigger and MaintenanceTrigger triggers.

于 2014-09-01T20:40:51.603 回答