Controlling music from lock screen | MPNowPlayingInfoCenter

Gurpreet Oberoi
3 min readJan 31, 2020

--

Now a days, shortcuts are too important for our life. It has become a human nature to apply shortcuts for each and every task if possible, and if we can achieve something with less effort then :

“Why not to apply shortcuts.”

Music is something that gives beauty in the form of smile on faces.One of the mostly used and most trending applications are the music applications. Each and every one of us has one or the other music application in our phones that helps us to release our stress. 😌

Now we are going to merge the above mentioned statements and direct the story towards iOS development 🙂 . At preset, the market of application development has become a bit competetive. New clients and newly emerging products want to beat or touch the big beasts that are holding a great market share. To achieve this they have to provide great UI and a great UX to their application.

MPNowPlayingInfoCenter adds a great UX in a music application. 😎

If we are building a music application, the basic music controls plays the most important role, and these controls are as follows:

Play,

Pause,

Manage Seek(slider)

Play Next

Play Previous

volume(+)

volume(-)

It will be really very frustrating for the users if they will have to open music application again and again to perform these small and important operations.

MPNowPlayingInfoCenter provide shortcuts for performing these operations.

Step1: The first requirement is to start receiving remote control events so the users can control the music from playback controls on the lock screen:

UIApplication.shared.beginReceivingRemoteControlEvents()

Step2: Add targets to all the required controls:

func addActionsToControlCenter(){addActionToPlayCommand()addActionToPauseCommnd()addActionToPreviousCommand()addActionToNextCommand()addActionToChangePlayBackPosition()addActionToseekForwardCommand()addActionToseekBackwordCommand()}func addActionToPlayCommand(){MPRemoteCommandCenter.shared().playCommand.isEnabled = true   MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(playCommand))}func addActionToPauseCommnd(){
MPRemoteCommandCenter.shared().pauseCommand.isEnabled = trueMPRemoteCommandCenter.shared().pauseCommand.addTarget(self, action: #selector(pauseCommand))
}func addActionToPreviousCommand(){MPRemoteCommandCenter.shared().previousTrackCommand.isEnabled = true MPRemoteCommandCenter.shared().previousTrackCommand.addTarget(self, action: #selector(previousButtonTapped))}func addActionToNextCommand(){MPRemoteCommandCenter.shared().nextTrackCommand.isEnabled = true
MPRemoteCommandCenter.shared().nextTrackCommand.addTarget(self, action: #selector(nextButtonTapped))
}func addActionToChangePlayBackPosition(){MPRemoteCommandCenter.shared().changePlaybackPositionCommand.isEnabled = trueMPRemoteCommandCenter.shared().changePlaybackPositionCommand.addTarget(self, action: #selector(changePlaybackPosition))}func addActionToseekForwardCommand(){MPRemoteCommandCenter.shared().seekForwardCommand.isEnabled = trueMPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(seekForward))}func addActionToseekBackwordCommand(){MPRemoteCommandCenter.shared().seekBackwardCommand.isEnabled = trueMPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(seekBackword))}

Step 3: Displaying current song information on lock screen:

func updateInfoCenter() {guard let item = currentItem else {return}var nowPlayingInfo : [String : AnyObject] = [MPMediaItemPropertyPlaybackDuration : item.duration ?? 0 as AnyObject,MPMediaItemPropertyTitle : item.contentObj.title as AnyObject,MPNowPlayingInfoPropertyElapsedPlaybackTime : item.currentTime as AnyObject,MPNowPlayingInfoPropertyPlaybackQueueCount :totalCount as AnyObject,MPNowPlayingInfoPropertyPlaybackQueueIndex : playIndex as AnyObject,MPMediaItemPropertyMediaType : MPMediaType.anyAudio.rawValue as AnyObject, MPMediaItemPropertyArtist]      MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo}

Here, currentItem is an AVPlayerItem that helps in playback. In addition, it also provides metadata that we are using to display the information on lock screen.

This data provided in MPNowPlayingInfoCenter.default can be dynamic according to the requirements of the application.

A BIG THANKS (❤) for reading till the end. 🙂

Heading forward with part2 of MPNowPlayingInfoCenter in which we will talk in detail with a working demo.

🥂

--

--