MPNowPlayingInfoCenter Archives — Now Playing Apps Let's build it together Sat, 21 Mar 2020 16:01:10 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 151952093 How to give more control to your users using MPNowPlayingInfoCenter https://nowplayingapps.com/how-to-give-more-control-to-your-users-using-mpnowplayinginfocenter/ Sat, 22 Sep 2018 18:28:58 +0000 https://nowplayingapps.com/?p=17 Building a video player to stream content is absolutely amazing, however your customer experience is only as good as how accurate the information that is presented to them. I have had challenges putting accurate data in front of the user, and here is how overcame this challenge when using AVPlayer in iOS apps.

The post How to give more control to your users using MPNowPlayingInfoCenter appeared first on Now Playing Apps.

]]>
Building an app that plays audio or video content? MPNowPlayingInfoCenter is a powerful API that Apple introduced (in iOS 5.0) that gives up to date information about your current playing content even if your app is backgrounded, or screen is locked. Let’s see what are the benefits of adding it to your app:

Benefits of adding NowPlayingInfo

  • Provide a quick overview of what your app is playing without opening your app
  • Includes all necessary metadata such as title, subtitle, start/end time, the artwork of current track and much more
  • Playback controls even if the app is in the background (such as play/pause, next/previous track)
  • Cast button to other airplay enabled devices
  • Visible on external devices (such as CarPlay) or other apple devices such as Apple TV or Apple Watch
  • Can be updated even if the app is running in the background

Let’s integrate

Alright, enough theory. Let’s dive in to see how your app can take advantage of this. MPNowPlayingInfoCenter has a singleton called NowPlayingInfo which is a dictionary where you can provide all this information (such as title, artist name, duration, artwork image etc.). That’s it! Easy as that. Let’s see it in code:

First things first. NowPlayingInfoCenter is part of MediaPlayer framework. So import it first like this:

import MediaPlayer

Alright, now lets us see what all properties are available to use.

var nowPlayingInfo = [String: Any]()
        
// prepare title and subtitle
nowPlayingInfo[MPMediaItemPropertyTitle] = "Movie Title"
nowPlayingInfo[MPMediaItemPropertyArtist] = "Artist Name"
        
// asynchronously download album art image.
// here, for simplicity, an image from asset catalog is loaded
if let albumArt = UIImage(named: "cover-art") {
    nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: albumArt.size, requestHandler: { imageSize in
        return albumArt
    })
}


If the current track is a live stream content (like news or a live event), you can take advantage of MPNowPlayingInfoPropertyIsLiveStream property.

// if playing item is a live stream. Default is false
nowPlayingInfo[MPNowPlayingInfoPropertyIsLiveStream] = true

To show current track duration and progress bar:

// Get asset duration from AVPlayerItem's AVAsset
// Duration is accurate only when AVPlayerItem.status == .readyToPlay.
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = playerItem.asset.duration.seconds
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = playerItem.currentTime().seconds


Please note that playerItem.asset.duration is accurate only once AVPlayer/AVAudioPlayer has loaded the stream URL and is ready to play. In order to do this, it is recommended to observe AVPlayerItem’s status property and checking whether it is AVPlayerItem.Status.readyToPlay. Refer to my blog post to read more about this.

And finally, assign our nowPlayingInfo dictionary to MPNowPlayingInfoCenter like this:

// final step is to assign our dictionary to 
// MPNowPlayingInfoCenter singleton
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo


One last thing. In order for the show the nowplayinginfo on the notification screen, you need to add this one last piece of code inside your AppDelegate’s didFinishLaunchingWithOptions function.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     application.beginReceivingRemoteControlEvents()
}

And there you go! Here is how it would look on an iPhone 7 Plus device:

and if your app supports Apple CarPlay, this is how it looks:

NowplayingInfo Carplay

Conclusion

This is a great feature to take advantage of for your iOS streaming app and gives control and accessibility to your users. Happy streaming!

Check out our new upcoming service, RenewMyPush. With this, we hope to take the burden off you to update the push certificate manually. If this is something that you are interested in, let us know by visiting the service page, and we will let you know once this service is ready.

The post How to give more control to your users using MPNowPlayingInfoCenter appeared first on Now Playing Apps.

]]>
17