且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MPRemoteCommandCenter控制中心的暂停按钮即使在音频文件暂停后也不会更新

更新时间:2023-02-03 10:13:10

对于iO 9.2 :

我已经尝试了MPNowPlayingInfoCenter和MPRemoteCommandCenter中几乎所有设置的组合.

I've tried nearly all combinations of settings in MPNowPlayingInfoCenter and MPRemoteCommandCenter.

停止更新,搜索栏就足以设置

MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0.0
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player?.currentTime

但是要切换暂停按钮,需要在暂停audioPlayback后停用AVAudioSession.

But to toggle pause button it is needed to deactivate AVAudioSession after pausing audioPlayback.

AVAudioSession.sharedInstance().setActive(false)

最后,我的pauseCommand处理程序看起来像:

Finally my pauseCommand handler looks like:

MPRemoteCommandCenter.sharedCommandCenter().pauseCommand.addTargetWithHandler { (e) -> MPRemoteCommandHandlerStatus in
        player?.pause()
        let infoCenter = MPNowPlayingInfoCenter.defaultCenter()
        infoCenter.nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0.0
        infoCenter.nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player?.currentTime

        _ = try? AVAudioSession.sharedInstance().setActive(false)

        return MPRemoteCommandHandlerStatus.Success
}

希望,这对某人有帮助.

Hope, this helps somebody.