Oranutachi
Oranutachi

Reputation: 381

How to correctly configure the display of track time in MPNowPlayingInfoCenter

I created MPNowPlayingInfoCenter and pass the current track time and total track time into it. But such a problem arises. If I pause the track in MPNowPlayingInfoCenter, wait for a while, and then press play, then I will see that the current time has changed, although it should have remained in the same place because the track was not playing. How to get around this?

...
func setupNotificationView(...) {
        nowPlayingInfo = [String : Any]()
        ...
 
        nowPlayingInfo?[MPMediaItemPropertyPlaybackDuration] = track.duration
        nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player.currentTime().seconds
        nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
        
        MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
    }
...
func setupMediaPlayerNotificationView() {
        let commandCenter = MPRemoteCommandCenter.shared()
        
        commandCenter.playCommand.addTarget { [unowned self] event in
            if self.player.rate == 0.0 {
                self.player.play()
                return .success
            }
            return .commandFailed
        }
        
        commandCenter.pauseCommand.addTarget { [unowned self] event in
            if self.player.rate == 1.0 {
                self.player.pause()
                return .success
            }
            return .commandFailed
        }
        ...
}

Upvotes: 1

Views: 520

Answers (1)

Carl Smith
Carl Smith

Reputation: 1360

I am able to stop the current time from changing by setting the MPNowPlayingInfoPropertyPlaybackRate value to zero with a literal constant when I pause the track, as shown below:

nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = NSNumber(floatLiteral: 0.0)

Upvotes: 1

Related Questions