How to handle currently playing background music in your application?

My application failed today because of certification rule 6.5 Application that Play Music section 6.5.1. Before I get into how to fix it here is the requirements and failed result.

6.5.1 Requirement

When the user is already playing music on the phone when the application is launched, the application must not pause, resume, or stop the active music in the phone MediaQueue by calling the Microsoft.Xna.Framework.Media.MediaPlayer class.

The application needs to prompt the user for consent to adjust the volume or stop the music that is currently playing in the Zune queue.

Expected Result

Test Process Required:

1. Play audio and/or music media.
2. Launch the application.
3. The application must not pause, resume, or stop the active music on the device.
4. If the application plays its own background music, the application must prompt for user
consent to stop playing or to adjust the currently playing background music.

Steps to reproduce:

1. Play any music on the device.
2. Launch the application.
3. Select one of the duck caller types displayed on the screen.
4. Notice the application stops the current music without prompting the user.

Using MediaPlayer.GameHasControl to fix the issue

This is very simple to address by using MediaPlayer.GamehasControl which is part of Microsoft.Xna.Framework.dll with namespace Microsoft.Xna.Framework.Media. It will return false if there are some background music currently being played and you must prompt the user for the permission to stop the background music and play your own sound.

Here is the code snippet from the DuckCaller application to handle this shown below:

private void PlaySound(string soundFile)

{

    bool canPlay = false;

 

    FrameworkDispatcher.Update();

    if (MediaPlayer.GameHasControl)

    {

        canPlay = true;

    }

    else

    {

        if (MessageBox.Show(“Is it ok to stop currently playing

music and play our duck caller sound?”, “Can play duck caller?”,

MessageBoxButton.OKCancel) == MessageBoxResult.OK)

        {

            canPlay = true;

        }

    }

 

    if (canPlay)

    {

        btnPlay.Visibility = System.Windows.Visibility.Collapsed;

        btnPause.Visibility = System.Windows.Visibility.Visible;

 

        _updatingMediaTimeline = true;

        mediaDuckSound.Stop();

        mediaDuckSound.Source = new Uri(soundFile, UriKind.Relative);

 

        mediaDuckSound.Position = System.TimeSpan.FromSeconds(0);

        mediaDuckSound.Volume = 1;

        mediaDuckSound.Play();

 

        ShowBuyNow();

    }

}

Conclusion

I am glad that Microsoft goes into this much detail to care about the user experience. This definitely will help in publishing the quality with consistent user experience.

About toetapz

Henry Lee is founder of NewAgeSolution.Net and is passionate about the technology. He is also the author of the book Beginning Windows Phone 7 Development 2nd from Apress (http://www.apress.com/9781430235965). He works with various Fortune 500 companies delivering mobile applications and rich internet applications. He recently formed start-up company called ToeTapz.com focusing his energy on delivering mobile applications in Windows Phone 7, Android and iPhone. In his spare time, he dedicates his effort to help his .NET community by delivering sessions at the technology events. He enjoys talking with other technologist about current trends in the technology and sharing business insights with fellow colleagues. Often you will find Henry at local cigar bar enjoying a cigar and a drink trying to come up with next big mobile application.
This entry was posted in Marketplace Certification, Windows Phone 7 and tagged , . Bookmark the permalink.

7 Responses to How to handle currently playing background music in your application?

  1. Chris says:

    Perfect – I had everything but the FrameworkDispatcher.Update()! Thank you very much.

  2. ryan says:

    One question though, I believe that article 6.5.3 states that you must resume the playing of the music that was previously playing if your app attempts to show video. How would you suggest going about this?

    “6.5.3 Applications that Play a Video or Audio Segment
    An application may interrupt the currently playing music to play a non-interactive full motion video or audio segment (e.g. cut-scene or media clip) without asking for user consent. If music was playing prior to the segment, the application must resume music when the segment has completed.”

    • toetapz says:

      This will apply equally to Video because you can not disrupt background music. Good way to test this is from your app press windows key which will tombstone out to of your app. And when you come back to your at it will resume and you have to handle the case otherwise you will get rejected.

  3. Pingback: How to handle Application test failed – Error: 6.5 Applications that Play Music? | Toetapz's Blog

  4. dachu02 says:

    i recieve an error:

    Error 1 The type or namespace name ‘Xna’ does not exist in the namespace ‘Microsoft’ (are you missing an assembly reference?)

    i use this:
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Media;

  5. sanjan says:

    how do you assign the name for media control items?
    and how to pass the song thats playing in the background to the method PlaySound?
    thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s