Using the Built-In System Music Player

Overview

Let’s say you have created an app and you want users to be able to access the apple music from your app. Yes, that is possible!!

Today, we will learn how to handle apple music action using our own application like Play, Pause, Remaining time, Total time, Elapsed time, etc. with album art. In short, learn to use all Apple music actions(Next, Previous) using our own created application.

What is System Music Player?

The system music player uses the built-in Music app on your behalf. On instantiation, it takes on the current Music app state, such as the identification of the now-playing item. If a user happens to switch away from your app while the music is still playing, that music will continue playing. The Music app then has your music player’s almost every detail such as most recently-set repeat mode, shuffle mode, playback state, and now-playing item.

Design

MP_image1

Code explained

Code Topics
  • Creating Outlets
  • Import Framework
  • Permission added in infoPlist file
  • Actions for Play, Pause, Next, Previous button

Creating Outlets

Import Framework

First, you’ll need to import media framework

Create a variable for the media player and timer

Permission added in infoPlist file

Now set the permission to access the Apple Music. And set in info.plist

Next we’ll need to add some code to the viewDidLoad function to get things going at startup, starting with getting the media player ready to go:

Next, you’ll need to set up the timer, this will come in the handle when a song is playing.

Actions for Play, Pause, Next, Previous button

We create some actions for play, pause, next and previous music.

Inside that function is where we are going to update all of the labels as well as get the slider to progress along with the song. Enter the following in the function:

This creates a constant for

so it’s easier to call while also ensuring that it exists before trying to pull the information. We’re going to need to pull a few key things before we can do anything, and the code should be pretty self-explanatory.

Now we set the timer’s interval to 1, the information will be updated every 1 second, so the currentPlaybackTime will constantly update as the song plays.

So what do we do with that information? First, let’s go ahead and set the image to the current track’s album artwork, which we’ve already gotten above.

We can also set the label above the slider to display the song’s artist and title.

Now calculate the total duration of song.

In Next step calculate the remaining and elapsed time.

Now run the application and test how it works.

MP_image2

MP_image3

Conclusion

Thus this is how easy it is to have all the apple music controls in your own app. Write back for any queries. Adios!

mm

Akshay Patel iOS Developer

Akshay is an iOS developer at Yudiz Solutions. He is passionate about learning new things and loves to explore when it comes to developing creative ideas into apps.

Comments are closed.

Top