3

The following is the code where I am initialising shared instance of 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))

The operation that is performed in the handlers such as HostPlayer.pauseCommand et al is to play/pause/previous/next the sound track depending upon the external accessory signal.Here is one such handler function.

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

Here, the player is an instance of MPMusicPlayerController.applicationMusicPlayer().

The problem is, even if I press certain buttons to play/pause from external accessories (such as AirPods), the relevant handler does not get executed or the program control never reaches there. A solution to this problem or any other workaround to that end would be much appreciated.

ANUVIND
  • 73
  • 4
  • Did you manage to solve it? I have similar problem, the forward button is working but not the backwards ... – XcodeNOOB Jun 10 '17 at 18:11

0 Answers0