Windows Phone 8.1 for developers – the Background Media Player

This blog post is part of a series about how Windows Phone 8.1 affects developers. The series is written in collaboration with Microsoft evangelist Peter Bryntesson, check out his blog here.

Dude… what is going on here?!

Right! You are developing a new music app on Windows phone 8.1 and nothing looks like it did a week ago. Gone is the MediaService to find the music, the BackgroundPlayer and all other classes that you were used to. This post is here to help you find your bearing with the new API’s.

Creating the background service

Adding the lib

Create a new Windows Runtime Component. This is where your entry point for the Audio task will be. The entry point is a class that implements IBackgroundTask. This gives you one method that will be invoked on startup of the Audio task named Run.

In order to keep the Task alive indefinitely you need to keep track of the deferral that exists on the IBackgroundTaskInstance. Placing the deferral in a field for safe keeping will be enough. When the operating system cancels your task you will need to call Complete on this said deferral.

An example of a bare bone implementation of your task should look something like the following

public sealed class AudioPlayer : IBackgroundTask
private BackgroundTaskDeferral _deferral;
public void Run(IBackgroundTaskInstance taskInstance)
        _deferral = taskInstance.GetDeferral();
        taskInstance.Canceled += TaskInstance_Canceled;
private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)

Note that right now the task isn’t really an audio player. But it is enough code to test that we have a functioning task. At least we will be able to test it, as soon as we have enabled it in the app manifest.

Enabling the background player

Open the app manifest. In Declarations add a BackgroundTask, then check the Audio task type and point the Entry Point to the AudioPlayer class that we created in the previous part. It should look something like the following


After this, we need to reference the Windows Runtime Component in the app project.

There is no need to use a BackgroundTaskBuilder for an Audio task. The way we start the background task is by simply by accessing BackgroundMediaPlayer.Current. You can verify this by adding a break point in the AudioPlayer class.

That’s it for the basic set up. Next up is how we use the code we have made to play some music.

Background player

Playing music

The new background player got three simple methods for playing songs. SetFileSource, SetStreamSource and SetUriSource. Only one of these can be called from the foreground application, namely the SetUriSource method. The others needs to be used from your task instead. The reason for this is that the background player don´t have access to the memory where SetFIleSource or SetStreamSource is located unless it is set in the task.

Play next song when the current song ended

In our AudioPlayer class we can hook up to the MediaEnded event. Here we can find the next song to play and start playing it.

Hey where is the “Tag”?! (Communication with the foreground app)

Tag is no more! The communication between the two processes is now done by using events.

Changing song from the foreground

If you want to change song from the foreground you just call:

BackgroundMediaPlayer.Current.SetUriSource(new Uri("ms-appx:///Assets/Media/Ring02.wma"));

Remember to use SetUriSource because the other overloads will throw exceptions.

Changing song from the background

To do this we need to tell the background to start playing a song. This is done by calling the BackgroundMediaPlayer function SendMessageToBackground:

BackgroundMediaPlayer.SendMessageToBackground(new ValueSet { { "play", "0" } });

The background AudioPlayer need to have set up to listen to the message in the Run method:

BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;

After receiving the message just do the same as in the forground to play songs.

Listen to event from the system

When your app is not active but your set up background player is the user can click on the player and expect your app to respond to these events. It can be for example play, pause, next and previous. To do this you need to set up this code in your AudioTask:

systemmediatransportcontrol = SystemMediaTransportControls.GetForCurrentView();
systemmediatransportcontrol.ButtonPressed += systemmediatransportcontrol_ButtonPressed;
systemmediatransportcontrol.PropertyChanged += systemmediatransportcontrol_PropertyChanged;
systemmediatransportcontrol.IsEnabled = true;
systemmediatransportcontrol.IsPauseEnabled = true;
systemmediatransportcontrol.IsPlayEnabled = true;
systemmediatransportcontrol.IsNextEnabled = true;
systemmediatransportcontrol.IsPreviousEnabled = true;

Then in the function systemmediatransportcontrol_ButtonPressed your app will receive the events from the phone. You can then from the AudioTask send an event to the forground using BackgroundMediaPlayer.SendMessageToForeground. This has to be set up in the foreground to work:

BackgroundMediaPlayer.MessageReceivedFromBackground += BackgroundMediaPlayer_MessageReceivedFromBackground;

You can now change your app if it is running to correspond to what the background is up to, changing song, pause etc.


This was a quick introduction on how to use the BackgroundMediaPlayer. It is a bit of work to get it to work but luckily there is a lot of help. Please look at for info and to see a working example.

This Post Has 17 Comments

  1. Monto

    How about making this work with silverlight 8.1 Projects?

  2. We have any Radio-Station-Apps and all work with the BackgroundMediaPlayer but only one do nothing, because the stream is loading from a SHOUTcast server and the stream contains meta-data. So is it possible to cut the meta-data from the stream and feed BackgroundMediaPlayer with the rest of stream-data?

  3. Michael

    Hi, thanks for this post.
    I tried this and it worked fine except that the UVC is not showing. The sound is still played even if I kill the app but the user can’t stop it.
    I couldn’t find out why. Do you have any idea ?

    1. Andreas Hammar

      Sorry no idea. Could be bugs.
      Post in the forum on – the teams monitor them.

      1. Michael

        Ok. I figured out that the Windows Runtime Component (Portable) is the problem. It allows to make the audio play in background but don’t manage the UVC. I used the same code in a Windows Runtime Component (WP8.1) and I have the UVC displayed.

  4. Billy

    The content of this article has disappeared. Can you maybe put it back?

    1. Robert Hedgate


  5. Guy

    One of the things that really suck with WP 8 is that in Android I can run a few media players, all running in background services, to create a multi channel app using mp3. In WP 8 I can only do this in the foreground and using only one mp3 stream because the classes that support playing a few audio tracks all demand wav files, which bloats my application considerably.
    Is there any improvement in WP 8.1 in that regard?


  6. behroozbc

    hi thanks for this post
    I have a problem when I do this my song play only 15 sec why ?

  7. Paul


    i’m trying to get the metadata of current track playing. From any app. Is this possible with the new Windows 10 API ?

  8. Quy

    I have a problem with my music app.(Windows 10 mobile app)
    When I play a song. If I press Window hard button. And a little bit, I back to my app. My app will be reload again.
    I was checked in system task manager. I saw system kill my app if I play song and suspend. It’s not happen when I’m not play song.
    Please help me!
    Sorry my bad english.
    Thanks so much!

Leave a Reply