9

One of my apps have recently failed certification because: "my app stops background music without asking user when it wants to play some music".

Now the question is: how can we detect if there is any music playing in the background?

Regards

Mo Valipour
  • 13,286
  • 12
  • 61
  • 87
  • 2
    I think you're missing the "without asking" clause. Don't go blurting out Aretha automatically, let the user put her finger on it first. Go Aretha! button, I meant. – Hans Passant Aug 11 '11 at 23:59
  • @HansPassant by pressing on the Play button in our app = user agrees to play ? – onmyway133 Dec 17 '12 at 05:47

2 Answers2

7
  using Microsoft.Xna.Framework.Media;

...

    if (Microsoft.Xna.Framework.Media.MediaPlayer.State == MediaState.Playing)
    {
          ....
    }
Damian
  • 4,723
  • 2
  • 32
  • 53
6

You need to examine the MediaPlayer.GameHasControl property.

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.media.mediaplayer.gamehascontrol.aspx

MediaPlayer.State will be Playing even if you're playing the music. GameHasControl determines if the music was started from your app, or if another app was playing before your app started.

You can get the value in OnActivated...

protected override void OnActivated(object sender, EventArgs args)
{
  base.OnActivated(sender, args);


  // cache music and trial mode values
  Globals.GameHasMusicControl = MediaPlayer.GameHasControl;

}

And use that value throughout your game to determine whether or not you should play music.

Dave Carlile
  • 7,347
  • 1
  • 20
  • 23