I\'m writing a basic music player app but having some problems when it comes to handling the app state transitions.
I\'m using Swift 3 and MPMusicPlayerController.sy
The "terminated due to signal 9" message just means your app was terminated by a SIGKILL signal. The OS sends that signal whenever your app is terminated involuntarily, whether it's because of memory pressure (or several other reasons not relevant to this discussion), or the user explicitly killing your app by double tapping the Home button and swiping it away.
In your case, the user is explicitly killing your application, so the "Terminated due to signal 9" message is completely expected. If your application is the current foreground application, your applicationWillTerminate method will get called, as shown in your logic flow outline above (Flow 2). If your application is NOT the current foreground application (Flow 1), your applicationWillTerminate
method will NOT get called if your application is in a suspended state. This is expected behavior. Also note the distinction between "background state" and "suspended state". They are not the same thing.
So if I'm understanding you correctly, it sounds like the problem is that the audio continues playing after your application is terminated by the user (Flow 1). That means you are doing something wrong in your handling of the MPMusicPlayerController
, because it should handle that state transition automatically.
Make sure you've set the correct UIBackgroundMode for your app. Setting the wrong background mode can cause your application to misbehave because the OS only allows certain operations while in background, depending on what background mode you've set. Setting the wrong mode (or trying to do things that are explicitly disallowed in the mode you've set) will cause your app to be suspended or terminated.
Make sure you've set up your audio session correctly.
Make sure you are responding correctly to the music player notifications - in particular, make sure you're calling beginGeneratingPlaybackNotifications
and endGeneratingPlaybackNotifications
appropriately, and that you are handling those notifications correctly. Check your notification handlers to make sure you aren't doing anything silly in there. Make sure your controller doesn't go out of scope or otherwise get released before you've called endGeneratingPlaybackNotifications
.
If you've done everything else correctly, an MPMusicPlayerController
pretty much manages itself, so you shouldn't have to do anything special to make it work when your app goes to the background (aside from setting the correct UIBackgroundMode
, of course). As a last resort, start commenting out code until your app is just a barebones "open-an-audio-file-and-play-it" application, and see if it exits properly then. If it does, you can start uncommenting code piece-by-piece until it fails - then you know what part of your app is causing the problem and you can narrow it down from there.
I was having three background task for the App.
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
<string>location</string>
<string>remote-notification</string>
</array>
Message from debugger: Terminated due to signal 9
This message comes when app is running into the background and it consumes more memory which beyond the allocated memory by the os of a iPhone for Background running apps.
In my case, I was updating the location of User and executing location api of the user to the server continuously. It was consuming lots of memory. Due to this reason, OS killed the App.
We got this message due to memory pressure on OS and killed the app in the background.
I optimised the code and whenever we need to update location of user then only we fired the location api to server.
I also enable \ disable
the flag allowsBackgroundLocationUpdates
if #available(iOS 9.0, *) {
coreLocationManager.allowsBackgroundLocationUpdates = false
}
according to our need.
it worked fine.
Fyi: If you make changes in Settings app for app permissions like Camera usage and photo usage, your app will also crash (hard refresh) with this signal.