我用两个不同的音频播放器(收音机和 MP3)创建了应用程序。为两个播放器使用 MPRemoteCommandCenter 从锁定屏幕管理播放器。这是代码片段:
MP3播放器
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.nextTrackCommand.isEnabled = true
commandCenter.nextTrackCommand.addTarget(self, action: #selector(next))
commandCenter.previousTrackCommand.isEnabled = true
commandCenter.previousTrackCommand.addTarget(self, action: #selector(previous))
commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.addTarget(self, action: #selector(playPause)
commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.addTarget(self, action: #selector(playPause))
收音机播放器
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.removeTarget(self, action:nil)
commandCenter.playCommand.addTarget(self, action: #selector(radioPlayFromLock))
commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.removeTarget(self, action:nil)
commandCenter.pauseCommand.addTarget(self, action: #selector(radioJPStop))
commandCenter.nextTrackCommand.isEnabled = false
commandCenter.previousTrackCommand.isEnabled = false
两者都可以单独正常工作,但请按照以下步骤操作:
- 播放 MP3 播放器
- 之后播放电台
- 并试图从锁定屏幕停止
- 项目清单
由于寻找 MP3 播放器方法而崩溃。这意味着目标方法不会针对播放/暂停命令进行更新。任何帮助将不胜感激。