Meet Up #3


We had four new members tonight! After we got to know them, we continued with our JustPlay practice app.

New Code:

  1. The app automatically plays a random song when it first launches, rather than playing a song after tapping the PLAY button.
  2. We Moved some of our code from our ViewController.Swift file to our AppDelegate.Swift file so that music starts playing much faster upon our app first launching.
  3. We added a NEXT button that skips to a new random song when tapped.

New Swift:

We now use a Switch statement in our @IBAction func actionButton() which switches on the current title of the sender. The sender is either the PLAY, PAUSE, or the NEXT button. This is our first time using a Switch statement. It was fun!

We also used the MPMusicShuffleMode and MPMusicPlaybackState properties of the MPMusicPlayerController class. We set the shuffle mode of our music player to the .Song value, which is actually one case of four cases of the MPMusicShuffleMode enumeration. We also check the playbackState of our music player to make sure its actually .Playing before we set the title of our button to read “PAUSE”.


  1. Switch Statements in Swift – See Switch under Control Flow in The Swift Programming Language Book by Apple
  2. MPMusicPlayerController Class Reference – See shuffleMode and playbackState under Managing Playing Mode and State, and Shuffle Modes and Playback States under Constants
  3. Value and Reference Types – Our music player is a reference type

Practice App Materials

Rather than launching the app then tapping the PLAY button to play a song, now when the app launches, it automatically plays a random song. We could have added this functionality into our ViewController.Swift file, but in order to make this happen even faster, we added the code to our application(didFinishLaunchingWithOptions) function in our AppDelegate.Swift file. As its name implies, this function is called as soon our application is finished launching, so its a great place to put this code.

Just like before, we create an instance of MPMusicPlayerController and set our music player’s queue with an MPMediaQuery, but this time we also set its shuffle mode to  .Songs, then we call .play(). ***An earlier version mistakenly said to not setQueueWithQuery.

Since our app automatically starts playing music when it launches, we changed our PLAY button to say PAUSE upon launch, but only after we check that our app is actually playing a song do we make the PAUSE button visible (as well as our NEXT button). We check this in the viewDidLoad method in our ViewController.Swift file by checking that the playbackState of our music player is .Playing.

The code for our PAUSE button is pretty much the same. The code for our PLAY button is the same too, but there is something interesting to note. We don’t need to set the shuffle mode anymore. Because classes in Swift are pass by reference, when we set the shuffle mode of our music player in our app delegate, the shuffle mode will stay that way until we change it again. Understanding the difference between value types and reference types are very important in Swift.

We use a Switch statement in our @IBAction func to Switch on the title of button. So, depending on which button is tapped, our app will respond differently. We have two buttons now. One button for PLAY & PAUSE, and another button for NEXT. We could have a separate @IBAction func for our NEXT button, but for such a simple app, our code is much cleaner this way.

The code for our NEXT button is new. We can’t just call play() when the NEXT button is tapped because we want the music player to play a different random song. So we solve this problem by setting the queue of our music app with a our media query. Because our music player’s shuffle mode is set to .Songs, it will play a random song from the media query. Also, we make sure the the PLAY button reads “PAUSE” anytime the NEXT button is tapped. We can’t user sender here because sender is the NEXT button, so we created an @IBOutlet property for our actionButton and call setTitle on it.

We also added an @IBOutlet for the NEXT button.

That’s pretty much it!

Here’s the code:

In the AppDelegate.Swift file, the applicationDidFinishLaunchingWithOptions method should look like this:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let myMusicPlayer = MPMusicPlayerController()
let myMediaQuery = MPMediaQuery.songsQuery()
myMusicPlayer.shuffleMode = .Songs
return true


***An earlier version mistakenly omitted setQueueWithQuery

Then ViewController.Swift should look like this:


import UIKit
import MediaPlayer

class ViewController: UIViewController {

@IBOutlet weak var actionButtonLabel: UIButton!
@IBOutlet weak var nextButtonLabel: UIButton!

let myMusicPlayer = MPMusicPlayerController()
let myMediaQuery = MPMediaQuery.songsQuery()

@IBAction func actionButton(sender: UIButton) {
let currentTitle = sender.currentTitle
switch currentTitle! {
case “NEXT”:
actionButtonLabel.setTitle(“PAUSE”, forState: nil)
case “PLAY”:
sender.setTitle(“PAUSE”, forState: nil)
case “PAUSE”:
sender.setTitle(“PLAY”, forState: nil)
default: break

override func viewDidLoad() {

if myMusicPlayer.playbackState == .Playing {
actionButtonLabel.hidden = false
nextButtonLabel.hidden = false

override func didReceiveMemoryWarning() {


Meet Up 3 Screencast