How to detect if another audio is playing in background? (Windows Phone 7)
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
using Microsoft.Xna.Framework.Media;
...
if (Microsoft.Xna.Framework.Media.MediaPlayer.State == MediaState.Playing)
{
....
}
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.
精彩评论