'AVAudioPlayerNode stops when the apps goes to background

I've implemented the audio EQ via AVAudioEngine and AVAudioPlayerNode and it is working fine (tried both scheduling a buffer or a file). However, once the app goes to background the sound just fades away. Background mode is correctly set, as is the audio session, and I've verified it by playing music with AVPlayer and then going to background). No audio engine notifications are received.

Here's the code for initializing the engine:

        let x = CrbnPlayerEQ()
        
        let eq = AVAudioUnitEQ(numberOfBands: 8)
        x.audioUnitEQ = eq
        x.audioUnitEQ?.globalGain = 0
        
        x.audioEngine.attach(eq)
        x.audioEngine.attach(CrbnPlayer.shared.player.nodePlayer)
        
        let mixer = x.audioEngine.mainMixerNode
        x.audioEngine.connect(CrbnPlayer.shared.player.nodePlayer, to: eq, format: mixer.outputFormat(forBus: 0))
        x.audioEngine.connect(eq, to: mixer, format: mixer.outputFormat(forBus: 0))
        
        try? x.audioEngine.start()

And here's the play part for the AVAudioPlayerNode:

            CrbnPlayerEQ.shared.audioEngine.prepare()
            try? CrbnPlayerEQ.shared.audioEngine.start()
            self.nodePlayer.stop()
            self.nodePlayer.scheduleFile(audioFile, at: nil) {
                
            }

The result remains the same when I use the scheduleBuffer instead of the scheduleFile. I've tried changing playback modes and audio session options but none of that helped. I've also tried stopping and starting the audio session when the app goes to background.

One solution would be to switch to the AVPlayer once the app goes to background but then I'd lose the EQ.

Does anyone know how to ensure the buffer keeps playing even after the app goes to background?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source