如何将当前AVAudioPlayer时间与Control Center同步?

时间:2017-02-17 16:44:25

标签: ios swift

我尝试了以下内容:

func setInfo() {
    guard let splayer = player else { return }
    let simage: UIImage = mainImage2!
    let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
        return simage
    })
    UIApplication.shared.beginReceivingRemoteControlEvents()
    let commandCenter = MPRemoteCommandCenter.shared()
    commandCenter.playCommand.isEnabled = true
    commandCenter.pauseCommand.isEnabled = true
    commandCenter.playCommand.addTarget(self, action: #selector(playMusic))
    commandCenter.pauseCommand.addTarget(self, action: #selector(pauseMusic))
    commandCenter.nextTrackCommand.addTarget(self, action: #selector(next(_:)))
    commandCenter.previousTrackCommand.addTarget(self, action: #selector(previous(_:)))
    MPNowPlayingInfoCenter.default().nowPlayingInfo = [MPMediaItemPropertyTitle: songText, MPMediaItemPropertyArtwork: artwork, MPMediaItemPropertyPlaybackDuration: splayer.duration, MPNowPlayingInfoPropertyPlaybackRate: 1.0]
    timer1?.invalidate()
}

它有效,但是当我寻找轨道时,它不会更新时间。请帮助我,以便不断更新?谢谢!

1 个答案:

答案 0 :(得分:0)

控制中心完全独立于您的应用。所有这一切都是根据您在开始时配置它的持续时间来计算秒数。当声音暂停或恢复或改变位置时,由来更新MPNowPlayingInfoCenter,告诉它当前的播放位置是什么。你显然没有这样做。