I just took a basic Swift 2.0
course. I am trying to make an app to select a song from iOS's Music app library and play it. I came across this link which shows how to make media item picker.
import UIKit
import MediaPlayer
class ViewController: UIViewController {
@IBOutlet weak var pickSong: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let mediaPicker = MPMediaPickerController(mediaTypes: .Music)
// mediaPicker.delegate = self
// mediaPicker.prompt = "Select song (Icloud songs must be downloaded to use)"
mediaPicker.allowsPickingMultipleItems = false
mediaPicker.showsCloudItems = false
presentViewController(mediaPicker, animated: true, completion: {})
}
mediaPicker.delegate = self
line shows
Cannot assign value of type 'ViewController' to type 'MPMediaPickerControllerDelegate?'
error message. When I blocked it, the app works and allow me to browse songs perfectly.
Question 1: I would like to know what is the use of this line?
Question 2: How to play a song that I picked using this code?
I searched here and other websites for how to play songs. I found people are using
player.play()
to play music. I tried that and failed.