mattcarlson
mattcarlson

Reputation: 37

Grabbing song info from MPMediaItemPicker

I am attempting to assign the song title, author, and seconds played of a song to a set of variables. However I am unable to pull any information from the song, what would be the best way to do this as currently my way crashes.

func presentPicker (sender:AnyObject) {
//I have all of this within an IBAction if that matters, I am new to programming in general so sorry if theres any stupid mistakes
    let mediaPicker = MPMediaPickerController(mediaTypes: .Music)
    mediaPicker.delegate = self
    mediaPicker.allowsPickingMultipleItems = false
    presentViewController(mediaPicker, animated: true, completion: {println(MPMediaItemCollection())})

}

Upvotes: 0

Views: 66

Answers (1)

Dash
Dash

Reputation: 17428

You shouldn't be using the completion argument of presentViewController:. The completion runs when the controller successfully presents, but you want to grab the song when it's finished dismissing. You need to implement this delegate method in your class:

func mediaPicker(mediaPicker: MPMediaPickerController!,
    didPickMediaItems mediaItemCollection: MPMediaItemCollection!) {
    println(mediaItemCollection)
}

That method will be called on your class when the user has selected a song because you set delegate equal to self. You may also want to implement this one to find out if they cancelled the picker:

func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController!)

Upvotes: 1

Related Questions