How to update MPNowPlayingInfoCenter/MPRemoteCommandCenter play/pause controls with AVAudioPlayerNode?

204 Views Asked by At

I am making a simple music player app that just plays audio using AVAudioEngine. When I pause the AVAudioPlayerNode, it does not update the play/pause control of the MPNowPlayingInfoCenter/MPRemoteCommandCenter. How do I update it?

I dont want to pause the entire AVAudioEngine and then resume to update the MPNowPlayingInfoCenter/MPRemoteCommandCenter play/pause control since it lags up my UI and there's a delay when playing back audio.

Does anyone have any idea or solution regarding this topic? The documentation is absolutely atrocious and no one knows a goddamn thing about AVAudioEngine. Anyone?

func pause() {
        playerNode.pause()
        audioEngine.pause() 
        
        displayLink?.isPaused = true
        
        DispatchQueue.main.async {
            self.musicPlayerControlsManager.isPlaying = false
        }
    }
    
    
    func playPlayerNode() {
        if !audioEngine.attachedNodes.isEmpty && audioFile != nil {
            
            DispatchQueue.main.async {
                self.musicPlayerControlsManager.isPlaying = true
                self.displayLink?.isPaused = false
            }
            
            do {
                try audioEngine.start()
                playerNode.play()
                
            } catch {
                print(error.localizedDescription)
            }
        }
    }
1

There are 1 best solutions below

0
On

You really do seem to need to stop all audio to influence the now playing lockscreen widget, so in your case that does in fact mean calling pause() on your AVAudioEngine. Maybe you can mitigate the restart lag by preloading/buffering.