Windows Phone - Play a music in the background across pages

by rahul 4/5/2013 11:42:23 PM

Developing for Windows Phone can be fun and rewarding at the same time. While working on Kidculator, I thought it would be better to have an app with a modern UI and some music playing in the background. I found two minor roadblocks which I would like to share with you today [in case you get stuck too!].

1. How to ensure that your music plays seamlessly across different pages in a Windows Phone app?
2. How to ensure that when you suspend your application / tombstone / press your Windows Button on phone, your music resumes playing?

I didn't realize it was going to be as tough as it acted to be.

So, without further ado, the steps...

    Inside App.xaml -> <Application.Resources> block, add the following... Notice that in this case my music file is called mymusic.wav and is stored inside sounds folder

<MediaElement x:Name="ME" MediaEnded="RepeatMedia" Volume="1" AutoPlay="False" Source="/Sounds/mymusic.wav" />

    Paste the following method...

        public MediaElement player = null;
        private void InitializeMusic()
        {
            if (App.Current.Resources.Contains("ME"))
            {
                player = App.Current.Resources["ME"] as MediaElement;
            }
            player.MediaOpened += player_MediaOpened;
        }
        void player_MediaOpened(object sender, RoutedEventArgs e)
        {
            player.Play();
        }
        private void RepeatMedia(object sender, RoutedEventArgs e)
        {
            player.Play();
        }

    Now, call InitializeMusic...

        // Code to execute when the application is launching (eg, from Start)
        // This code will not execute when the application is reactivated
        private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            InitializeMusic();
        }
        // Code to execute when the application is activated (brought to foreground)
        // This code will not execute when the application is first launched
        private void Application_Activated(object sender, ActivatedEventArgs e)
        {
            InitializeMusic();
        }

    So, by now... you will have a sound playing in the background. You can check by navigating to multiple pages and the music should play seamlessly.
    At this point, although it may seem like your application is working well... try pressing the Windows Key on your phone, and the app will be tombstoned or suspended.
    When you long press the back key and restore this application back, you will not be able to hear the music yet!
    To fix this, modify your InitializeMusic()...

        private void InitializeMusic()
        {
            if (App.Current.Resources.Contains("ME"))
            {
                player = App.Current.Resources["ME"] as MediaElement;
                player.Source = new Uri("/Sounds/mymusic.wav", UriKind.RelativeOrAbsolute);
            }
            player.MediaOpened += player_MediaOpened;
        }

    
    Notice that the source needs to be added again. I don't know why though. Thanks to keyboardP for his help!
    All set. It should work well now!

Hope this helps, Wave
Rahul

 


Quote of the day:
"I like long walks, especially when they are taken by people who annoy me." - Noel Coward

Tags:

Windows Phone 7 | Windows Phone 8

blog comments powered by Disqus

Disclaimer

The opinions expressed herein are our own personal opinions and do not represent our employer's view in any way.
© Copyright 2014, Rahul Soni