Compartir a través de


WP7 Dev Tip: Detecting whether or not the user is playing music in the background

Applies To:  Silverlight & XNA

Quick Bits

Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl

The Setup

Your app does some sort of music playing that doesn't make sense to blend into any already playing background music (for example, you're going to stream your own music) or you simply want to show a video.

Why You Care?

If you just start playing music you will fail certification according to section 6.5.1 of the certification guide if you don't ask the user if you can stop the background music, but how can you stop the background music if you don't know it's playing? Hence, you care!

The Solution

Query Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl (it's a bool) - if you have control, then you're good to go. If you don't have control, then there is something in the background and you need to prompt the user before continuing. 

Silverlight Note: you'll need to link in Microsoft.Xna.Framework.dll for this to work, but make sure you don't distribute this file with your XAP by mistake (or you'll also fail certification for redistributing phone assemblies)

Comments

  • Anonymous
    December 01, 2010
    Oren, can I use Xna.Framework in Silverlight Application for WP7?

  • Anonymous
    December 02, 2010
    Hey Alex, Yes, you can link to Xna.Framework and then use the Media namespace. The certification guide has the following to say about using Xna libraries from Silverlight: "4.2.5 The application must not call any APIs in the Microsoft.Xna.Framework.Game assembly or the Microsoft.Xna.Framework.Graphics assembly when using any methods from the System.Windows.Controls namespace." There's a lot of useful libraries in XNA that you can use from Silverlight, especially the different media manipulation ones (for example, you can play media as a SoundEffect (Micorosoft.Xna.Framework.Audio.SoundEffect) to have it mixed in with whatever is current playing in the background. Hope that helps!