If your Windows Phone application needs to play some sort of music but you don’t know whether the device is already playing it in the background, what will you do?

 

Today in this small Tips & Tricks we will learn how to detect whether music is playing in your Windows Phone 7 device. When the player is playing media files in the background, you can easily detect it by following some simple steps.

 

Problem Statement

Let’s assume you are developing a game or a media player application. While building your app or game, you noticed that while a media is running in the background, your app is also playing music and creating a blend over the existing music. So, what will you do?

 

If you submit this game or app in the Windows Phone Marketplace, it will automatically fail the certification steps as mentioned in the certification guide.

 

Solution

There is a small trick by which you can rectify this and pass your app for Windows Phone Certification and publish it in the Marketplace. To resolve this, you need to add a local reference of “Microsoft.Xna.Framework.dll” in your project first and then you need to call the “Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl” property to determine whether the game/app has control over the music player.

 

Here is the sample code that you need to implement:

 
bool gameHasControl = Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl;
if (gameHasControl)
{
    // Music player is not playing in the background
}
else
{
    // Music player is playing in the background
}

 

If the property returns “True”, that means the player is not playing and “False” when music is playing. Once you get to know whether the media is playing, you can prompt the user to take necessary action before continuing. This way, you can pass your application certification in the Windows Phone Marketplace too. Also make sure that, you are not distributing the assembly as part of the XAP file, otherwise it will also fail the certification process.

 

Don’t forget to connect with me on Twitter and Facebook for technical updates and articles news. Also subscribe to my blog’s Newsletter to get all the updates delivered directly to your inbox. We won’t spam or share your email address as we respect your privacy.

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.