Starting to work on my iOS port and I’m a bit confused about something, how does the backgrounding work with monogame? With other platforms, I know where to trigger code to pause my game, however I don’t know what the main trigger is on iOS when the user presses the home button and the application is minimized.
Not sure if you already figured this out, but the solution is to use Game.Deactivated and Game.Activated for pausing and unpausing, respectively. You use them just like you would with XNA:
Game.Deactivated gets called whenever iOS fires applicationWillResignActive, which will always happen before your app gets backgrounded, and Game.Activated gets called whenever iOS fires applicationDidBecomeActive, which will always happen when your app gets foregrounded.
Don’t think this works… I tried hooking my own event handler onto Game.Deactivated, but in tests I could hear that the game was running for a second or two after I had pressed the home button.
I then stuck a breakpoint into Game.OnDeactivated in Monogame.IOS’s Microsoft.Xna.Framework.Game class and ran my test again - this event does not fire until the game has been out of focus for a couple of seconds.
Is there a way to directly trap the home button being pressed at the moment it happens? Or some other way to go round the problem?
I’ve noticed that too, and I think it may have to do with a quirk of the iOS multitasking system. If you try double-tapping the Home button, you’ll see that the Activated/Deactivated code fires instantly. But if you just press Home once to do a full backgrounding, it seems like iOS decides to be lazy and waits to deactivate so that there’s a smoother/fancier background animation with fading music.
But only sometimes. I have code in my game that tries to pause the music on deactivate, and sometimes the music does pause and sometimes it just fades out.
I have spent DAYS looking into this, and I have no idea why it happens. I’ve tried bypassing MonoGame’s activated/deactivated methods and just wiring directly into Apple’s methods: applicationDidBecomeActive, applicationWillResignActive, applicationDidEnterBackground, applicationWillEnterForeground. But to no avail. No combination of these methods seems to allow me to pause my music reliably on background with my MonoGame game.
The strange thing is that this seems to work more reliably with a lightweight non-MonoGame test app, but I have no idea if it’s the smaller app size/memory footprint or if it’s because I’m not using MonoGame. FWIW, I’ve looked through MonoGame’s background code pretty thoroughly, and I can’t see any problems, so who knows? This may just be an irritating quirk of iOS.
@Picogames “This may just be an irritating quirk of iOS.” - I think you’re right. Downloaded and played a couple of games, they exhibit the same behaviour. So unless I fail certification because of it, I’m not going to worry too much.
Fastball 2 does it… can’t remember the others, don’t have my test devices to hand right now.
“you’ll definitely pass certification” - oh boy, I hope so! I’m running out of time before release date, and Apple’s sign-up processes are proving more of a pain than I ever anticipated!