img

How to enable Background audio in multiple pages in Windows phone application

In my last post I had explained you how to play a media file in Windows phone app using Media element control.

http://www.windowsapptutorials.com/windows-phone/media/play-audio-file-windows-phone-app-using-media-element-control/

 

But when you want to play a continuous audio in the background you may face a problem as you navigate from one page to another as music will stop when you leave the page where the mediaelement control is defined. To overcome this problem one way is to define the Mediaelement globally. So in this post I shall be explaining you about how to define a global media element in Windows phone application.

 

1. First create a new blank project GlobalMediaelement.

 


 

2. Then add a new page to the application having the name Page1.xaml.

 


 

3. Now we need to add a Gloabal Media Element to our Application. So Open the page App.xaml and add the following code given below.

 

 


 

4. Now open the page App.xaml.cs and add the following code given below globally in the page.

 

 


 

This code is used to define the Mediaeneded event handler of the Mediaelement which will restart the song as it ends. You can manipulate it according to your needs.

 

5. Now add three buttons on the MainPage. The first button is used to play the audio, second one is used to stop the audio and third one is to navigate to Page1.xaml.The code for click event handlers of the buttons are given below.

 

Play button

 

 

Stop button

 

 

Navigate button

 

 

6. Now run the application on emulator. And Click on the play button. You will hear a background audio running in your application.

 


 

Now as you navigate on to Page1.xaml you will see that Background music will not stop and will be played continuously.

I hope this post will be helpful to you. You can download the full project source file from the link given below.

 
Download full project source file GlobalMediaElement.zip
 

Chetan Gulati

Currently am doing B TECH in IT from Delhi Technological University. App development is my passion. Believe in quality rather than quantity.

Read more:
How to create your first Windows Phone app
How to change the App name and Tile name of your Windows phone app
Data binding in Windows Phone app – One way binding
Close