MPNowPlayingInfoCenter – Control the music player in the Lock screen

Tram Ho

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:

  1. Media playback
  2. Remote commands configuration
  3. 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:

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

3. Playing info center configuration

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:

https://medium.com/@g4gurpreetoberoi/controlling-music-from-lock-screen-mpnowplayinginfocenter-3f75ec7972d6

https://medium.com/@varundudeja/showing-media-player-system-controls-on-notification-screen-in-ios-swift-4e27fbf73575

Share the news now

Source : Viblo