如何在MPNowPlayingInfoCenter中正确配置曲目时间的显示



我创建了MPNowPlayingInfoCenter,并将当前曲目时间和总曲目时间传递到其中。但这样的问题出现了。如果我在MPNowPlayingInfoCenter中暂停曲目,等待一段时间,然后按播放,我会看到当前时间已经更改,尽管它应该保持在同一位置,因为曲目没有播放。如何绕过这个?

...
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
}
...
}

当我暂停曲目时,我可以通过将MPNowPlayingInfoPropertyPlaybackRate值设置为零并使用文字常量来阻止当前时间的更改,如下所示:

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

最新更新