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

41 Views Asked by At

I have created a media player plugin using 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 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
        }
        
        // Add handler for Pause Command
        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.

0

There are 0 best solutions below