Creating a Music Player App in Swift 3

Erica Millado
Yay It’s Erica
Published in
4 min readJul 25, 2017
What’s your favorite Prince song?

In this blog, I’ll walk through my steps in creating a music player app that plays one of my favorite songs: Purple Rain ☔️. In this app, we’ll be using AVFoundation, which is an Apple framework that allows us to record/play/edit photos, video and audio.

Step 1: Create the UI

For my UI, in Storyboard, I dragged a Toolbar with three buttons. In Attributes Inspector, I changed the buttons to Systems Items with a PLAY button, a PAUSE button, and typed “Restart” for my last button.

I also made sure that I dragged Flexible Space Bar Button Items between each of my three buttons.

Step 2: Create IBActions for these buttons.

Control-drag from each of the 3 buttons in Storyboard to your ViewController file.

Make sure each connection is an Action, not an Outlet.

Step 3: Select the song!

I have the song, Purple Rain, downloaded from iTunes so I will drag it from iTunes into my Project navigator pane (just under the Main.storyboard) file. Copy items if needed.

When it appears in my Navigator pane, I rename it to something easy, like PurpleRain. The file type is .m4a. DO NOT CHANGE THIS TO mp3. When I tried changing it to mp3, bad things happened.

Step 4 & 5: Import AVFoundation and create an AVAudioPlayer.

Like I mentioned earlier, AVFoundation is Apple’s framework that allows you to play/edit/create videos/photos/audio. From Apple Documentation:

The AV Foundation framework provides an Objective-C interface for managing and playing audio-visual media in iOS and macOS applications.

We import this framework at the top and create an instance of an AVAudioPlayer class, which does exactly that, it plays audio from a file.

Step 6: Write a function to setup our song and session.

From Apple Documentation:

An audio session is used to communicate to the system how you intend to use audio in your app. An audio session acts as an intermediary between your app and the operating system — and in turn, the underlying audio hardware.

I wrote a function, prepareSongAndSession( ) to handle this functionality:

Step 7–13: Set up our AVAudioPlayer and an AVAudioSession.

Since I manually dragged the Purple Rain song from my iTunes into the project, it is now part of my project Bundle.

#7 — I insert the song (from the Bundle) into my AVAudioPlayer. I “!” banged it because I know for sure that the song exists in the project. This action throws an error, hence the do/try/catch block.

Go to your Build Phases/Copy Bundle Resources to make sure that your music file IS INDEED there. If not, drag it from the Navigator pane into the Copy Bundle Resources folder.

#8 — I call the prepareToPlay( ) method on my songPlayer.

#9 — I create an AVAudioSession, which communicates to the system how I am going to use it (to play Prince!)

#10 — Set the category of our audioSession to AVAudioSessionCategoryPlayback which is the category needed for playing recorded music. This action throws and error, hence the do/try/catch block.

#11 & 12 — print/handle errors from each catch block.

#13 — call this function in viewDidLoad( ).

Step 14: Call the .play( ) method on our songPlayer.

Step 15 & 16: Handle the pause functionality.

#15 — I created a boolean hasBeenPaused with a default value of false that will manage the state of the pause functionality.

#16 — In my pause( ) function, if the songPlayer is currently playing, I pause the songPlayer and change the hasBeenPaused bool to true.

Step 17: Handle the restart functionality.

I check to see if my songPlayer is currently playing OR if it has been paused and stop the songPlayer. I reset the songPlayer to a current time of zero and then resume playing.

Build and run.

You can find the repo for this project here.

Resources:

AVAudioPlayer — Apple Documentation

AVAudioSession — Apple Documentation

--

--