How to detect if another audio is playing in backg

2019-01-15 13:59发布

问题:

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

回答1:

  using Microsoft.Xna.Framework.Media;

...

    if (Microsoft.Xna.Framework.Media.MediaPlayer.State == MediaState.Playing)
    {
          ....
    }


回答2:

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.