In this blogpost, I’m going to discuss the following things:
- Make an app that plays music
- Play music when the app is not in foreground
- Control music when the app is not in foreground
- Display current song, artist, album

Lets play some music

1. We make a new project and include the following libraries:
- AVFoundation
- AudioToolbox

2. Make new viewcontroller. In my case I’ll name it ‘PlayViewController’

3. Go to your storyboard and make some outlets for the following controls and connect them to our ‘PlayViewController’:
- Previous button (Type: UIButton)
- Next button (Type: UIButton)
- Play button (Type: UIButton)
- Imageview for album art (UIImageView)
After doing all this, your interface should look like this:

4. Add included frameworks to your implementation file

5.Getting an audiosession and setting its category
The AVFoundation framework contains a singleton instance of ‘AVAudioSession’. This object handles all soundinput (such as recording) and output of the app and you can set different categories to this object according to the app’s purpose:

 Audio session categoryDefinition
 AVAudioSessionCategoryRecordAudio recording is the primary purpose. App continues recording when the screen is autolocked.
 AVAudioSessionCategoryPlayAndRecordFor audio playing and recording at the same time or not, i.e., voice over IP This setting will mute the other application’s audio.
 AVAudioSessionCategoryAudioProcessingNot playing audio or recording but processing the audio, such as format conversion.
 AVAudioSessionCategoryPlaybackAudio playing is the primary purpose in this application. Won’t allow other applications to play along. Won’t be muted even if the Silent switch is on. Supports background audio playing.

For my app, I’m going to select the AVAudioSessionCategoryPlayback category because I’m going to use the background audio playing functionality.

Setting the audiocategory is done very easily. The method for setting the category returns YES or NO if successful.

6. Play the music
Before we can play a sound, we must have some sounds. I’ve included some demo sounds in my project (named: Sound 1.mp3, Sound 2.mp3, Sound 3.mp3)

Now make 3 IBActions and connect them to the 3 buttons you just added to the viewcontroller’s view. These functions are named:
- previousButtonPressed
- nextButtonPressed
- playButtonPressed

These functions control what happens when a user presses one of the 3 buttons on screen. The actual code for playing music is included in the demo project I attached in this blog post.

This was first part of this blog post BUT we still have some issues we should fix:
- When closing the app, the music stops, even though we set our AVAudioSession’s category to playback.
- We still can’t control our music from outside the app.

Playing music when app is not in foreground

When you want your app to be able to play music is the background, you just have to enable a setting in your app’s plist-file. After setting this, your app’s music will no longer be paused when you press the home button.

Control music when app is not in foreground

When we want to respond to remote events in our app, we have to register for it.
That’s done like this:

In viewDidAppear:

Register this viewcontroller for reveicing remote events

What about [self becomeFirstResponder] in the above code?
You must register the current viewcontroller as the app’s first responder, so iOS knows which viewController is able to handle remote events. By overriding this method and returning ‘YES’, your viewcontroller is now the first one in row to receive remote events.

The actual receiving:

Just call the functions that were normally triggered by our own play, next and previous buttons.

That was all for the remote receiving of events! But what about showing to current song, artist, album outside of the app? The image below shows it:

Add the MediaPlayer.framework to our project

When playing a new song add the following code:

That was all! This code sets the current songname, artist and album. Easy isn’t it?

Playing music in the background only works on a device, not in the simulator.

Stay tuned for more!