Command Center media buttons is not syncing with App's media state Swift

I have created a media player plugin and exported it via swift framework for my Unity project, the media player works fine on the app.

But, when the App's media state is paused the Command Center media button does not update its still on the pause icon and same as with playing the command center button does not update.

I already have initiated the AVAudioSession shared instance and set the category to .playback and have also set the .setActive to true.

let audioSession = AVAudioSession
                .sharedInstance()
            try audioSession
                .setCategory(
                    .playback,
                    mode: .default
                )
            print("Playback OK")
            try audioSession.setActive(true)
            print("Session is Active")

I also have initialised the command center, for handling the play and pause events whenever the buttons are clicked in command center.


self.commandCenter.playCommand.isEnabled = true
        self.commandCenter.playCommand.addTarget { [weak self] (event) -> MPRemoteCommandHandlerStatus in
            self?.play()
            return .success
        }
        
self.commandCenter.pauseCommand.isEnabled = true
  self.commandCenter.pauseCommand.addTarget { [weak self] (event) -> MPRemoteCommandHandlerStatus in
            self?.pause()
            return .success
        }

This is the code for the handling the pause

self.nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(self.player.currentTime())
            self.nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = CMTimeGetSeconds(self.player.currentItem!.duration);
            self.nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 0
            self.nowPlayingInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = 0
            
            MPNowPlayingInfoCenter.default().nowPlayingInfo = self.nowPlayingInfo
self.player.pause()

and This is the code for handling the play

self.nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds(self.player.currentTime())
            self.nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = CMTimeGetSeconds(self.player.currentItem!.duration);
            self.nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 1
            self.nowPlayingInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = 1
            MPNowPlayingInfoCenter.default().nowPlayingInfo = self.nowPlayingInfo;
self.player.play()

What i am expecting is when i click on pause in the App the command center's button should be the play icon if I click on play the command center's button should be pause icon, but right now nothing is working.

Please tell me what I am missing here?

Your app needs to update the control state, enable/disable, based on its control state. I have this function that handles a change in the player state

    /// updates the media player controls to reflect the new player status
    /// - Parameter status: player status flags
    func updateUI(status: PlayerStatus) {
        ModuleData.singleton.mediaCommandCenter.playCommand.isEnabled = status.intersects(with: .play)
        ModuleData.singleton.mediaCommandCenter.stopCommand.isEnabled = status.intersects(with: .stop)
        ModuleData.singleton.mediaCommandCenter.pauseCommand.isEnabled = status.intersects(with: .pause)
        ModuleData.singleton.mediaCommandCenter.nextTrackCommand.isEnabled = status.intersects(with: .skipForward)
        ModuleData.singleton.mediaCommandCenter.togglePlayPauseCommand.isEnabled = status.intersects(with: [.stop, .play, .pause])
        ModuleData.singleton.mediaCommandCenter.previousTrackCommand.isEnabled = status.intersects(with: .skipBackward)
        ModuleData.singleton.mediaCommandCenter.skipForwardCommand.isEnabled = status.intersects(with: .skipBackwardTime)
        ModuleData.singleton.mediaCommandCenter.skipBackwardCommand.isEnabled = status.intersects(with: .skipBackwardTime)
        ModuleData.singleton.mediaCommandCenter.seekForwardCommand.isEnabled = status.intersects(with: .seekForward)
        ModuleData.singleton.mediaCommandCenter.seekBackwardCommand.isEnabled = status.intersects(with: .seekBackward)
    }

The status values are a bit mask enum with various controls enabled/disabled.

Command Center media buttons is not syncing with App's media state Swift
 
 
Q