Background Audio

As explained in Chapter 13, “Audio, Video, and Speech,” if you switch away from your app while it plays an audio file, the audio instantly fades out. When you switch back, it fades back in. If you want the audio to continue being audible while your app is offscreen, you need to do three simple things:

1. Set the AudioCategory appropriately on the MediaElement playing the audio.

2. Add an appropriate Background Tasks declaration to your package manifest.

3. Write some code to keep the MediaElement in sync with a class called SystemMediaTransportControls.

Setting the AudioCategory

MediaElement’s AudioCategory property can be set to influence how Windows treats the audio. To enable audio to play in the background, you must set ...

Get Universal Windows® Apps with XAML and C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.