3

以下是我初始化 MPRemoteCommandCenter 共享实例的代码。

    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
    MPRemoteCommandCenter.sharedCommandCenter().playCommand.enabled = true
    MPRemoteCommandCenter.sharedCommandCenter().playCommand.addTarget(self, action: #selector(HostPlayer.playCommand))
    MPRemoteCommandCenter.sharedCommandCenter().pauseCommand.enabled = true
    MPRemoteCommandCenter.sharedCommandCenter().pauseCommand.addTarget(self, action: #selector(HostPlayer.pauseCommand))
    MPRemoteCommandCenter.sharedCommandCenter().nextTrackCommand.enabled = true
    MPRemoteCommandCenter.sharedCommandCenter().nextTrackCommand.addTarget(self, action: #selector(HostPlayer.nextTrackCommand))
    MPRemoteCommandCenter.sharedCommandCenter().previousTrackCommand.enabled = true
    MPRemoteCommandCenter.sharedCommandCenter().previousTrackCommand.addTarget(self, action: #selector(HostPlayer.previousTrackCommand))

在诸如 HostPlayer.pauseCommand 等处理程序中执行的操作是根据外部附件信号播放/暂停/上一个/下一个音轨。这是一个这样的处理函数。

    func pauseCommand () -> MPRemoteCommandHandlerStatus {
    if player.playbackState == MPMusicPlaybackState.Playing {
    player.pause()
    return MPRemoteCommandHandlerStatus.Success
    }
    return MPRemoteCommandHandlerStatus.CommandFailed
    }

这里,播放器是 MPMusicPlayerController.applicationMusicPlayer() 的一个实例。

问题是,即使我按下某些按钮从外部配件(例如 AirPods)播放/暂停,相关的处理程序也不会执行或程序控制永远不会到达那里。对此问题的解决方案或任何其他解决方法将不胜感激。

4

0 回答 0