Playing Timed Sound Effects in Background

Hi, I'm relatively new to iOS development and kindly ask for some feedback on a strategy to achieve this desired behavior in my app.

My Question:

What would be the best strategy for sound effect playback when an app is in the background with precise timing? Is this even possible?

Context:

I created a basic countdown timer app (targeting iOS 17 with Swift/SwiftUI.). Countdown sessions can last up to 30-60 mins. When the timer is started it progresses through a series of sub-intervals and plays a short sound for each one. I used AVAudioPlayer and everything works fine when the app is in the foreground. I'm considering switching to AVAudioEngine b/c precise timing is very important and the AIs tell me this would have better precision.

I'm already setting "App plays audio or streams audio/video using AirPlay" in my Plist, and have configured:

AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: .mixWithOthers)

Curiously, when testing on my iPhone 13 mini, sounds sometimes still play when the app is in the background, but not always.

What I've considered:

  • Background Tasks: Would they make any sense for this use-case? Seems like not if the allowed time is short & limited by the system.
  • Pre-scheduling all Sounds: Not sure this would even work and seems like a lot of memory would be needed (could be hundreds of intervals).
  • ActivityKit Alerts: works but with a ~50ms delay which is too long for my purposes.
  • Pre-Render all SFX to 1 large audio file: Seems like a lot of work and processing time and probably not worth it. I hope there's a better solution.

I'd really appreciate any feedback.

Playing Timed Sound Effects in Background
 
 
Q