Enable background audio for multiple pages in Windows Phone 7 – Take 2

Update: Use with caution, added TestProject.

A couple of weeks ago I was working on a problem with handing audio over multiple pages in an application. It worked out well until the updated toolkit that came out in the beginning of November. I tried to add page transitions and after a day of frustration I found that my MediaElement injection broke the ability for page transitions.

The last solution was based on creating a ControlTemplate with a MediaElement in and ad that as the Template for the root frame, this breaks the ability for page transitions. As soon as I identified the problem I started to look for a solution. The solution was even simpler than I first anticipated. Instead of the injection I just added the MediaElemet as a resource in the App.xaml and then exposing it as a property. The code looks like this for the xaml part:


The code behind part is even simpler, it looks like this:

public static MediaElement GlobalMediaElement
  get { return Current.Resources["GlobalMedia"] as MediaElement; }

And you access it as before by calling it up in the page you need it on

var AppMediaElement = App.GlobalMediaElement;

There, simpler, cleaner and works as good as the first solution, and now I finally can start to use the page transitions again, sweet.

Update: As the MediaElement is very expensive it should be used with caution and here I will instantiate it at App level. In my case it is a player that Will play music and stream from the net so the purpose is to have it playing as long as I have music playing. However if you are not using it continuously you should not have a MediaElement like this. 

Update 2: I included a little testproject with code samples for this as well, that shows it works for both streams and included audio and should work for IsoStoraged files

Test project included above.

This Post Has 9 Comments

  1. Adi

    I tried putting the above code but it said that the attribute key from the XAML is not defined. I think my understanding isnt right here so what exactly do I do?

  2. Adi

    Sorry got the problem. I want the audio to play continously and loop and never stop how do I do that?

  3. Håkan Reis

    You could do it by hooking up an event handler on the mediaelement and detect when it has ended. Then just play it again.

  4. David Jacob


    Thanks for the great post. The solutions is working like wonder. However, i am facing one issue and hope that you can take me out of it.

    I am playing .mp3 file in the background and want it run throughout the application. It is running fine 2 or 3 times but then stop working. The mp3 file is around 17 seconds. Is there a way to resolve this? I want to play it continuously till game is closed or sound setting is done as off.

    Thanks in advance.

      1. Rommel

        Robert, that works if the top level element is a VideoElement. The preoblm arises when the VideoElement is wrapped in layers of ProxyElements, SerialElements and the like. Then you’d have to loop through the whole structure to find the VideoElements. By hijacking the element in resolveItems, I can check the elements at the very core of the onion of elements and set smoothing there.

  5. ken

    Thank you so much for the code…. it works.


  6. EngDev

    I want to play audio at background ,how can i do that

    1. Håkan Reis

      Well, this is kind of an old post. Since version 7.5 (Mango) you have the audio service to rely on so you can handle the background audio. Look up the samples on MSDN for details.

Leave a Reply