简体   繁体   English

WP8.1如何在C#中取消后台音频任务

[英]WP8.1 How to cancel a Background Audio Task in C#

I would like to cancel a background audio task from the app/background task in wp8.1 - is there a way to raise cancellation from code? 我想从wp8.1中的app / background任务中取消后台音频任务 - 有没有办法从代码中提取取消? can't find anything on msdn or anywhere else.. 在msdn或其他地方找不到任何东西..

Background why i want to do that: 背景为什么我想这样做:
In wp8 a lot of users weren't very amused about the fact that the systemmediacontrols were active all the time. 在wp8中,很多用户对系统控制一直处于活动状态这一事实并不感到好笑。 Same in wp8.1: Microsoft says the background task should be cancelled (i) when another app wants to use background audio, or (ii) after timeout (i think i read it somewhere). 在wp8.1中也是如此:微软说应该取消后台任务(i)当另一个应用程序想要使用背景音频时,或者(ii)超时后(我想我在某处读取)。 Since in my tests it seems that this timeout cancellation never happens, i would like to give the user a possibility to shutdown the hole app (incl. background audio). 因为在我的测试中似乎永远不会发生这种超时取消,我想让用户有可能关闭漏洞应用程序(包括背景音频)。

I would be grateful for any suggestion. 我会很感激任何建议。
Best 最好

As for BackgroundMediaPlayer you can call BackgroundMediaPlayer.Shutdown , as it is said here at MSDN : 对于BackgroundMediaPlayer,您可以调用BackgroundMediaPlayer.Shutdown ,如MSDN所述:

BackgroundMediaPlayer.Shutdown closes the media pipeline and release the MediaPlayer object from memory. BackgroundMediaPlayer.Shutdown关闭媒体管道并从内存中释放MediaPlayer对象。 If you try to access a reference to BackgroundMediaPlayer.Current again after calling Shutdown, you will get an error. 如果在调用Shutdown后再次尝试访问BackgroundMediaPlayer.Current的引用,则会出现错误。 Shutdown is meant for an app to clean the media pipeline when its task is cancelled. 关闭意味着应用程序在其任务被取消时清理媒体管道。

So remember to detach all handlers from Foreground-Background communication and suppress from calling BackgroundMediaPlayer.Current as it will start it again. 因此,请记住从Foreground-Background通信中分离所有处理程序并禁止调用BackgroundMediaPlayer.Current,因为它将再次启动它。

The typical way to handle cancellation is via passing a CancellationToken . 处理取消的典型方法是通过CancellationToken For details, see Cancellation in Managed Threads on MSDN. 有关详细信息,请参阅MSDN上的托管线程中的取消

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM