【问题标题】:Updating Target Action Method of Player remote control events (MPRemoteCommand)更新播放器远程控制事件的目标动作方法(MPRemoteCommand)
【发布时间】:2018-01-14 10:12:14
【问题描述】:

我创建了具有两个不同音频播放器(收音机和 MP3)的应用程序。为两个播放器使用 MPRemoteCommandCenter 从锁定屏幕管理播放器。这是代码sn-p:

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. 之后播放 Radio
  3. 并试图从锁定屏幕停止
  4. 列表项

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

【问题讨论】:

    标签: ios iphone swift audio mpremotecommandcenter


    【解决方案1】:

    而不是

    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

    【讨论】:

    • 在答案周围添加一点散文会提高清晰度。一开始我没看几遍就搞不清楚答案是什么。
    猜你喜欢
    • 2015-07-14
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多