formatC
formatC

Reputation: 83

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? can't find anything on msdn or anywhere else..

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. 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). 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

Upvotes: 3

Views: 1359

Answers (2)

Romasz
Romasz

Reputation: 29792

As for BackgroundMediaPlayer you can call BackgroundMediaPlayer.Shutdown, as it is said here at MSDN:

BackgroundMediaPlayer.Shutdown closes the media pipeline and release the MediaPlayer object from memory. If you try to access a reference to BackgroundMediaPlayer.Current again after calling Shutdown, you will get an error. 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.

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564413

The typical way to handle cancellation is via passing a CancellationToken. For details, see Cancellation in Managed Threads on MSDN.

Upvotes: 0

Related Questions