Meet Up #2

Recap

We had one new member tonight. After their introduction we picked up where we left off with our practice app, JustPlay. We decided to add the ability to pause the song. Now, when the app launches, the button reads “PLAY”. When we tap the PLAY button, the first song plays and the button changes to read “PAUSE.” Then when we tap the PAUSE button, the song pauses and the button changes back to read “PLAY”. It was super fun!

We used the UIButton Class Reference to learn about how to get and set the button title, and we used the Media Player Framework Reference to pause the song.

Resources

Practice App Materials

We continued building features into our JustPlay app. This time we added the ability to pause the song. We did this by changing the title of the button (the sender) to say “PAUSE” after the “PLAY” button is tapped. Then we used an “else if” statement to handle the case when the button is tapped while the button reads “PAUSE”.

Essentially, our app has two states right now: PLAY and PAUSE. When the app launches, the app is in a pause-like state and the button reads “PLAY”. When the PLAY button is tapped, the app is in a play state and the button reads “PAUSE”. We are using the current title property of the button to keep track of these two states. By keeping track of the states, we can change the result of tapping the action button.

There are two results of tapping the button, and the results depend on the two states:

  1. When the current title of the button reads “PLAY” and the button is tapped we play the first song and then set the title of the button to read “PAUSE”
  2. When the current title of the button reads “PAUSE”, we pause the song and set the button title to read “PLAY”.

During our practice, we checked the state of our app by using “sender.titleLabel.text”. I realize now that a more simple way to do this is to use “sender.currentTitle”.

I have included below the code to make this work. The new code (the code from Meet Up #2) is in bold. Comments are marked with //. The rest of the code is from Meet Up #1:


import UIKit
import MediaPlayer

class ViewController: UIViewController {

// We declared these two properties outside of the actionButton function that way they are created upon the launching of our app as opposed to every time the button is tapped.

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

// Now that the button pauses AND plays, we changed the name of the button action to “actionButton”, rather than just playButton.

// When we change the name of the function, we have to reconnect our button object in Interface Builder to our @IBAction function “actionButton” in our ViewController.Swift file. We also need to delete the old connection to “playButton”. We do this by selecting the button object in Interface Builder, locating the old connection in the connections inspector, and clicking the “x” button on the connection. 

// We also change “sender: AnyObject” to “sender: UIButton” because AnyObject doesn’t have a “currentTitle” property.

@IBAction func actionButton(sender: UIButton) {

// If the current title of the button (the sender) is “PLAY”, then do what we did in Meet Up #1

if sender.currentTitle == “PLAY” {

myMusicPlayer.setQueueWithQuery(myMediaQuery)
myMusicPlayer.play()

// Then set the title of the sender (the button) to “PAUSE”. Don’t worry about “forState: nil”

sender.setTitle(“PAUSE”, forState: nil)

// But, if the current title of the sender is “PAUSE”, then we pause our music player and then set the title of the button to “PLAY”

} else if sender.currentTitle == “PAUSE” {

myMusicPlayer.pause()

sender.setTitle(“PLAY”, forState: nil)

}

}  // <— A previous version was missing this closing curly brace!

override func viewDidLoad() {
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}


That’s it! Fun, right? I will upload a screencast soon. Hope this is helpful.

Advertisements