title: How to prevent iOS app to be suspended in the background during the call?
body: <p>I am developing an iOS app that plays audio and video files using AVPlayer. The app needs to play media files also when the app is backgrounded.</p>
  <p>I successfully enabled background play by using Audio, AirPlay &amp; background mode and configuring AVAudioSession with this snippet in my AppDelegate:</p>
  <pre><code>let session = AVAudioSession.sharedInstance()
          do {
              try session.setCategory(AVAudioSession.Category.playback,
                                      mode: AVAudioSession.Mode.default,
                                      options: [AVAudioSession.CategoryOptions.mixWithOthers])
              try session.setPrefersNoInterruptionsFromSystemAlerts(true)
              try session.setActive(true)
          } catch let error as NSError {
              print(&quot;Failed to set the audio session category and mode: \(error.localizedDescription)&quot;)
          }
  </code></pre>
  <p>App works fine in the background until a call interrupts the audio session. For the duration of the call app gets suspended. When I finish the call app is restored and the AVPlayer resumes playing the file.</p>
  <p>This behaviour is expected if I play the audio on my iPhone. But if I am streaming the media over AirPlay to my AppleTV the playing should not be interrupted. When I am streaming over AirPlay, the audio session interruption suspends the app and the app gets disconnected from AirPlay. App reconnects to the AppleTV and resumes playing when I finish the call.</p>
  <p>I tested the same scenario in YouTube and Spotify apps and streaming over AirPlay does not get interrupted by calls.</p>
  <p>Does anybody know how I can prevent calls from interrupting AirPlay streaming?</p>