When you make a music player, you don’t want to always open the music app right from the home screen. Sometimes you will want to lock your phone just to listen to music, but you still want to control the music player like listening to the next track, previous track or a little bit of information …
Music
Music is something that brings beauty in the form of a smile on a face. One of the most used and most trending apps is the music app. Each of us has one or another music app in our phone to help us relieve stress.
Theme
Now I will show you how to control music player on lock screen.
Necessary and sufficient conditions to apply in the application
To play music and get controls your iOS apps need the following:
- Media playback
- Remote commands configuration
- Playing info center configuration
1. Support playing music in background
For when the iOS app in the background can still play music, you need the following registration code:
1 2 3 4 5 6 7 | do { //keep alive audio at background try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category(rawValue: AVAudioSession.Category.playback.rawValue)) try AVAudioSession.sharedInstance().setActive(true) } catch _ { } |
Turn on Audio in Background Modes
2. Remote Commands
MPNowPlayingInfoCenter
is a class that allows interaction between application and remote control at the lock screen. This class provides the MPRemoteCommand
to handle in its application.
- First register this Remote in the application using the following command
UIApplication.shared.beginReceivingRemoteControlEvents()
- Handle functions you want
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 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 = true MPRemoteCommandCenter.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 = true MPRemoteCommandCenter.shared().changePlaybackPositionCommand.addTarget(self, action: #selector(changePlaybackPosition)) } func addActionToseekForwardCommand(){ MPRemoteCommandCenter.shared().seekForwardCommand.isEnabled = true MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(seekForward)) } func addActionToseekBackwordCommand(){ MPRemoteCommandCenter.shared().seekBackwardCommand.isEnabled = true MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(seekBackword)) } |
3. Playing info center configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 } |
Note
There are also many other things you can handle through the MPRemoteCommandCenter
such as seek, duration, elapsed, rate …
Hope this article can be of some help to you.
Source: