Unexpected behavior with AVPlayer and background audio on iOS 16
I'm optimizing some code but I'm testing a new approach and Hey everyone, I'm running into an issue that's driving me crazy... I'm working on a personal project and I'm working on an iOS app that plays background audio using AVPlayer, but I'm encountering an issue where the audio playback stops unexpectedly when the app goes into the background. I've set up the audio session correctly, but it seems like iOS is pausing the playback. I've tried adding the necessary background capabilities in the app's Info.plist, but it hasn't resolved the issue. Hereβs a snippet of how I configure the AVAudioSession: ```swift import AVFoundation class AudioManager { var player: AVPlayer? func setupAudioSession() { do { let audioSession = AVAudioSession.sharedInstance() try audioSession.setCategory(.playback, mode: .default, options: [.mixWithOthers]) try audioSession.setActive(true) } catch { print("Error setting up audio session: \(error.localizedDescription)") } } func playAudio(url: URL) { player = AVPlayer(url: url) player?.play() } } ``` I've also confirmed that I've enabled the "Background Modes" capability in Xcode and checked "Audio, AirPlay, and Picture in Picture". However, the playback still stops when the app goes to the background. The console logs show the following messages before the audio stops: ``` 2023-10-10 12:34:56.789 MyApp[12345:67890] [AVAudioSession] Audio session has been interrupted. 2023-10-10 12:34:56.790 MyApp[12345:67890] [AVAudioSession] Audio session interrupted, reason: 1. ``` I've tried adding observers for the audio session interruptions like this: ```swift NotificationCenter.default.addObserver(self, selector: #selector(handleInterruption(notification:)), name: AVAudioSession.interruptionNotification, object: nil) ``` And handling it with: ```swift @objc func handleInterruption(notification: Notification) { guard let userInfo = notification.userInfo, let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt, let type = AVAudioSession.InterruptionType(rawValue: typeValue) else { return } if type == .began { // Handle interruption began print("Audio interruption began") } else if type == .ended { // Handle interruption ended print("Audio interruption ended") if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt { let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue) if options.contains(.shouldResume) { player?.play() } } } } ``` Despite these efforts, the audio still does not resume correctly after the interruption. Has anyone faced a similar issue or can suggest what I might be missing here? Iβm testing this on an iPhone running iOS 16.0. This is part of a larger web app I'm building. Has anyone else encountered this? My development environment is Linux. Has anyone else encountered this? This is part of a larger service I'm building. Any help would be greatly appreciated! This is my first time working with Swift latest. Thanks for taking the time to read this! I'm working with Swift in a Docker container on Ubuntu 22.04. Could this be a known issue?