title: Quitting app causes error "Message from debugger: Terminated due to signal 9"
body: <p>I'm writing a basic music player app but having some problems when it comes to handling the app state transitions.</p>
  
  <p>I'm using Swift 3 and MPMusicPlayerController.systemMusicPlayer()</p>
  
  <p>The goal is this:</p>
  
  <p>1) Keep music playing when user taps the Home button and app enters bg (works)</p>
  
  <p>2) Stop the player ( myMP.stop() ) if the user the quits the app (works sometimes, throws error other times)</p>
  
  <p>I traced the flows using print statements based on possible actions and got this:</p>
  
  <p><a href="https://i.stack.imgur.com/0PciQ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/0PciQ.png" alt="enter image description here"></a></p>
  
  <p>Flow 2 is what I would expect, but Flow 1 throws an error
  when the app is closed - I would expect "will terminate" here.</p>
  
  <p>EDIT: The main issue is that when exiting the app using Flow 1, "will terminate" is never called - therefore "myMP.stop()" is never called and the player continues to play after the app has exited.</p>
  
  <p>There is a distinct difference in behavior if you click Home once (Flow 1) or double (Flow 2) while the app is active.</p>
  
  <p>Why do I get two different responses from what <em>should be</em> the same actions?</p>
  
  <p>EDIT:
  Most importantly, how do I stop the MediaPlayer for Flow 1 if it never gets to "will terminate" ?</p>
  
  <p>EDIT:</p>
  
  <p>Here is some sample code that replicates the issue:</p>
  
  <p>AppDelegate.swift</p>
  
  <pre><code>//
  //  AppDelegate.swift
  //  Jumbo Player
  //
  
  import UIKit
  //import MediaPlayer
  
  //doesn't matter where this is declared - here or in ViewController - same results
  //let myMP:MPMusicPlayerController = MPMusicPlayerController.systemMusicPlayer()
  
  @UIApplicationMain
  class AppDelegate: UIResponder, UIApplicationDelegate {
      var window: UIWindow?
  
      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool {
          // Override point for customization after application launch.
  
          return true
      }
  
      func applicationWillResignActive(_ application: UIApplication) {
          // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
          // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
          print("applicationWillResignActive")
      }
  
      func applicationDidEnterBackground(_ application: UIApplication) {
          // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
          // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
          print("applicationDidEnterBackground")
      }
  
      func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
          print("applicationDidReceiveMemoryWarning")
      }
  
      func applicationWillEnterForeground(_ application: UIApplication) {
          // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
          print("applicationWillEnterForeground")
      }
  
      func applicationDidBecomeActive(_ application: UIApplication) {
          // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
          print("applicationDidBecomeActive")
      }
  
      func applicationWillTerminate(_ application: UIApplication) {
          // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
          print("applicationWillTerminate")
          myMP.stop();
      }
  }
  </code></pre>
  
  <p>ViewController.swift</p>
  
  <pre><code>//
  //  ViewController.swift
  //  junkplayer
  //
  
  import UIKit
  import MediaPlayer
  
  let myMP:MPMusicPlayerController = MPMusicPlayerController.systemMusicPlayer()
  
  class ViewController: UIViewController {
  
      @IBOutlet weak var xxx: UIButton!
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
  
          let qrySongs = MPMediaQuery.songs()
          myMP.setQueue(with: qrySongs)
  
      }
  
      @IBAction func playbut(_ sender: UIButton) {
          myMP.play()
      }
  }
  </code></pre>
  
  <p>Download the project here: <a href="http://www.NextCoInc.com/public/junkplayer.zip" rel="noreferrer">www.NextCoInc.com/public/junkplayer.zip</a></p>