Windows Phone 8 - 2 Background Audios Clash and both of the App terminates

笑着哭i 提交于 2019-12-01 21:33:19

Use gameHasControl to check for other BAP using music:

bool gameHasControl = Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl;
if (!gameHasControl)
{
   MessageBox.Show("stopping other player"); // normally you should ask if to do that      
   BackgroundAudioPlayer.Instance.Stop();
}

Once it is Stopped, when you start your BAP, then old instance invokes Shutdown(), and your BAP will be new Instance, which you can normally use. The same is when your BAP is in memory and you start to play from Music+Video Hub.

Only watch out, because when you use XNA, you sometimes need to do:

FrameworkDispatcher.Update();

Otherwise your App will sometimes crash. Good luck.

EDIT - after comment

To make it work you need to add a reference to Microsoft.Phone.BackgroundAudio or use like this:

Microsoft.Phone.BackgroundAudio.BackgroundAudioPlayer.Instance.Stop();

BackgroundAudioPlayer is a Singleton which you are allowed to use - in this case - Stop it playing (of course give the choice to the User).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!