title: iOS: Start playing sound from background
body: <p>I need to start playing sound when user closes app. I use <code>applicationDidEnterBackground</code> method. Here it is:</p>
  
  <pre><code>func applicationDidEnterBackground(application: UIApplication) {
      let dispatchQueue =
      dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
  
      dispatch_async(dispatchQueue, {[weak self] in
  
          var audioSessionError: NSError?
          let audioSession = AVAudioSession.sharedInstance()
          NSNotificationCenter.defaultCenter().addObserver(self!,
              selector: "handleInterruption:",
              name: AVAudioSessionInterruptionNotification,
              object: nil)
  
          audioSession.setActive(true, error: nil)
  
          if audioSession.setCategory(AVAudioSessionCategoryPlayback,
              error: &amp;audioSessionError){
                  println("Successfully set the audio session")
          } else {
              println("Could not set the audio session")
          }
  
          let filePath = NSBundle.mainBundle().pathForResource("MySong",
              ofType:"mp3")
  
          let fileData = NSData(contentsOfFile: filePath!,
              options: .DataReadingMappedIfSafe,
              error: nil)
  
          var error:NSError?
  
          /* Start the audio player */
          self!.audioPlayer = AVAudioPlayer(data: fileData, error: &amp;error)
  
          /* Did we get an instance of AVAudioPlayer? */
          if let theAudioPlayer = self!.audioPlayer{
              theAudioPlayer.delegate = self;
              if theAudioPlayer.prepareToPlay() &amp;&amp;
                  theAudioPlayer.play(){
                      println("Successfully started playing")
              } else {
                  println("Failed to play")
              }
          } else {
              /* Handle the failure of instantiating the audio player */
          }
          })
  
  }
  func handleInterruption(notification: NSNotification){
  /* Audio Session is interrupted. The player will be paused here */
  
  let interruptionTypeAsObject =
  notification.userInfo![AVAudioSessionInterruptionTypeKey] as! NSNumber
  
  let interruptionType = AVAudioSessionInterruptionType(rawValue:
    interruptionTypeAsObject.unsignedLongValue)
  
  if let type = interruptionType{
    if type == .Ended{
  
      /* resume the audio if needed */
  
    }
  }
  
  }
  </code></pre>
  
  <p>func audioPlayerDidFinishPlaying(player: AVAudioPlayer!,
      successfully flag: Bool){</p>
  
  <pre><code>  println("Finished playing the song")
  
    /* The flag parameter tells us if the playback was successfully
    finished or not */
    if player == audioPlayer{
      audioPlayer = nil
    }
    }
  </code></pre>
  
  <p>It does not work. After debugging I see that <code>theAudioPlayer.play()</code> returns <code>false</code>. If I run this code for example in <code>applicationDidFinishLaunching</code> it plays sound. I added background mode <code>App plays audio or streams audio/video using AirPlay</code> to my <code>Info.plist</code> What's wrong here?</p>