How to detect whether Music is playing in WP Background


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.


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

5 comments

  1. always i used to read smaller articles or reviews which as well clear their motive, and that is also happening with this piece of writing which I read now.

    ReplyDelete
  2. Windows 8 does not support XNA lib , how to detect music playing on background there?

    ReplyDelete
  3. Is it possible to run hidden app in background for windows phone 8?

    ReplyDelete


 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top