1

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.

I successfully enabled background play by using Audio, AirPlay & background mode and configuring AVAudioSession with this snippet in my AppDelegate:

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("Failed to set the audio session category and mode: \(error.localizedDescription)")
        }

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.

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.

I tested the same scenario in YouTube and Spotify apps and streaming over AirPlay does not get interrupted by calls.

Does anybody know how I can prevent calls from interrupting AirPlay streaming?

Marko Ilic
  • 11
  • 1
  • Not exactly the same scenario but wonder if these answers here might be worth a shot https://stackoverflow.com/questions/13206952/is-there-a-way-to-keep-airplay-running-if-the-device-auto-sleeps-auto-locks/14823268#14823268 – Shawn Frank Jan 20 '22 at 15:11

0 Answers0