0

我用两个不同的音频播放器(收音机和 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

两者都可以单独正常工作,但请按照以下步骤操作:

  1. 播放 MP3 播放器
  2. 之后播放电台
  3. 并试图从锁定屏幕停止
  4. 项目清单

由于寻找 MP3 播放器方法而崩溃。这意味着目标方法不会针对播放/暂停命令进行更新。任何帮助将不胜感激。

4

1 回答 1

2

代替

commandCenter.playCommand.removeTarget(self, action:nil)

用这个:

commandCenter.playCommand.removeTarget(nil)//this actually removes your targets

来自 Apple 文档:

指定 nil 以删除所有目标。

https://developer.apple.com/documentation/mediaplayer/mpremotecommand/1622903-removetarget

于 2018-09-08T18:06:00.037 回答