【问题标题】:How to correctly configure the display of track time in MPNowPlayingInfoCenter如何正确配置 MPNowPlayingInfoCenter 中曲目时间的显示
【发布时间】:2020-11-22 02:45:46
【问题描述】:

我创建了 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
        }
        ...
}

【问题讨论】:

标签: ios swift avplayer mpnowplayinginfocenter


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-04-09
    • 1970-01-01
    • 2011-07-29
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多