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)