我用两个不同的音频播放器(收音机和MP3)创建了应用程序。使用MPRemoteCommandCenter为两个玩家管理来自锁定屏幕的玩家。以下是代码段:
MP3播放器
BootCompletedReceiver
广播播放器
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))
两者都是单独正常工作,但一旦按照以下步骤操作:
由于寻找MP3播放器方法而崩溃。对于播放/暂停命令,不更新该平均目标方法。任何帮助将不胜感激。
答案 0 :(得分:0)
代替
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