Swift 3 example with all woking commands, including changePlaybackPositionCommand :
func remotePlayerInit() { UIApplication.shared.beginReceivingRemoteControlEvents() let commandCenter = MPRemoteCommandCenter.shared() commandCenter.pauseCommand.addTarget(self, action: #selector(self.pauseSongTouch(_:))) commandCenter.playCommand.addTarget(self, action: #selector(self.playSongTouch(_:))) commandCenter.nextTrackCommand.addTarget(self, action: #selector(self.nextSongTouch(_:))) commandCenter.previousTrackCommand.addTarget(self, action: #selector(self.previousSongTouch(_:))) commandCenter.changePlaybackPositionCommand.addTarget(self, action: #selector(self.changedThumbSlider(_:))) setLockInfo() } func changedThumbSlider(_ event: MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus { audioPlayer.currentTime = event.positionTime setLockInfo() return .success } func setLockInfo() { let image = PlayerVC.songs[PlayerVC.currentSelection].image let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in return image }) let songInfo : [String : Any] = [MPMediaItemPropertyTitle : PlayerVC.songs[PlayerVC.currentSelection].name, MPMediaItemPropertyArtwork : artwork, MPNowPlayingInfoPropertyElapsedPlaybackTime : TimeInterval(audioPlayer.currentTime), MPNowPlayingInfoPropertyPlaybackRate : 1, MPMediaItemPropertyPlaybackDuration : audioPlayer.duration, MPMediaItemPropertyArtist : "Artist"] MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo } = [MPMediaItemPropertyTitle: PlayerVC.songs [PlayerVC.currentSelection] .name, MPMediaItemPropertyArtwork: artwork, MPNowPlayingInfoPropertyElapsedPlaybackTime: TimeInterval (audioPlayer.currentTime), MPNowPlayingInfoPropertyPlaybackRate: func remotePlayerInit() { UIApplication.shared.beginReceivingRemoteControlEvents() let commandCenter = MPRemoteCommandCenter.shared() commandCenter.pauseCommand.addTarget(self, action: #selector(self.pauseSongTouch(_:))) commandCenter.playCommand.addTarget(self, action: #selector(self.playSongTouch(_:))) commandCenter.nextTrackCommand.addTarget(self, action: #selector(self.nextSongTouch(_:))) commandCenter.previousTrackCommand.addTarget(self, action: #selector(self.previousSongTouch(_:))) commandCenter.changePlaybackPositionCommand.addTarget(self, action: #selector(self.changedThumbSlider(_:))) setLockInfo() } func changedThumbSlider(_ event: MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus { audioPlayer.currentTime = event.positionTime setLockInfo() return .success } func setLockInfo() { let image = PlayerVC.songs[PlayerVC.currentSelection].image let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in return image }) let songInfo : [String : Any] = [MPMediaItemPropertyTitle : PlayerVC.songs[PlayerVC.currentSelection].name, MPMediaItemPropertyArtwork : artwork, MPNowPlayingInfoPropertyElapsedPlaybackTime : TimeInterval(audioPlayer.currentTime), MPNowPlayingInfoPropertyPlaybackRate : 1, MPMediaItemPropertyPlaybackDuration : audioPlayer.duration, MPMediaItemPropertyArtist : "Artist"] MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo } Artist func remotePlayerInit() { UIApplication.shared.beginReceivingRemoteControlEvents() let commandCenter = MPRemoteCommandCenter.shared() commandCenter.pauseCommand.addTarget(self, action: #selector(self.pauseSongTouch(_:))) commandCenter.playCommand.addTarget(self, action: #selector(self.playSongTouch(_:))) commandCenter.nextTrackCommand.addTarget(self, action: #selector(self.nextSongTouch(_:))) commandCenter.previousTrackCommand.addTarget(self, action: #selector(self.previousSongTouch(_:))) commandCenter.changePlaybackPositionCommand.addTarget(self, action: #selector(self.changedThumbSlider(_:))) setLockInfo() } func changedThumbSlider(_ event: MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus { audioPlayer.currentTime = event.positionTime setLockInfo() return .success } func setLockInfo() { let image = PlayerVC.songs[PlayerVC.currentSelection].image let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in return image }) let songInfo : [String : Any] = [MPMediaItemPropertyTitle : PlayerVC.songs[PlayerVC.currentSelection].name, MPMediaItemPropertyArtwork : artwork, MPNowPlayingInfoPropertyElapsedPlaybackTime : TimeInterval(audioPlayer.currentTime), MPNowPlayingInfoPropertyPlaybackRate : 1, MPMediaItemPropertyPlaybackDuration : audioPlayer.duration, MPMediaItemPropertyArtist : "Artist"] MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo }
source share