2 years ago

#75302

test-img

Marko Ilic

How to prevent iOS app to be suspended in the background during the call?

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?

ios

swift

avplayer

avaudiosession

airplay

0 Answers

Your Answer

Accepted video resources