AVAudioPlayer init very slow on iOS 18

On Xcode 16 (16A242) app execution and UI will stall / lag as soon as an AVAudioPlayer is initialized.

let audioPlayer = try AVAudioPlayer(contentsOf: URL)
audioPlayer.volume = 1.0
audioPlayer.delegate = self
audioPlayer.prepareToPlay()

Typically you would not notice this in a music app for example, but it is especially noticable in games where multiple sounds are being played using multiple instances of AVAudioPlayer. The entire app slows down because of it.

This is similar to this issue from last year.

I have reported it to Apple in FB15144369, as this messes up my production games where fps goes down to nothing when sounds are enabled.

Unfortunately I cannot find a solution. Anyone?

Hello @Wizfinger, thank you for submitting a bug report. If you are playing multiple sounds in a game using a set of AVAudioPlayers, it might be best to initialize the players beforehand and cache them. If you have a lot of sounds, however, you might want to consider using AVAudioEngine in conjunction with a set of AVAudioPlayerNodes instead. You'd only need as many player nodes as the maximum number of simultaneous sounds you are playing, as you would then be able to cache the AVAudioFiles or AVAudioPCMBuffers for each sound.

Hi, I am using AVAudioengine for my game as well. In which i need for 5 sounds out of which 4 are same sounds just played one after another very fast.

95% of times, the sounds are working correctly.

But when sometimes, i use a lots of other competitor apps, games and return to my own app, i hear the sounds becoming glitchy in my app.

And as soon as i press lock button to use siri and return back instantly, the sounds start working correctly.

Similarly, this happens when we lock the device and come back to the app, which also rectifies the issue.

I tried serialqueues, global sync ( because the sounds are coming rapidly one after another), reduced CPU/memory loads drastically, tried recreating audio files in interruptions, appdidbecomeactive. didnt resolve.

Am using playback audio category for my app. Though when removed the audio category setup, this issue also happens on default setup of audio which ios provides for the app. Tried removing mixwithothers, didnt resolve.

Tried almost everything but i cant resolve this issue.

Even replaced the library of audio with a low latency one. that low latency sounds are working fine but taking lots of memory.

The Avaudioplayer setup keeps the memory not too much.

What could be the reason for this audio getting correct on opening siri and back and also on lock screen lock and unlock?

AVAudioPlayer init very slow on iOS 18
 
 
Q