0

Im looking for a way to use the MPMediaPickerController to allow the user to select a song. I want the user to be able to assign the songs file path to a button so that each time he/she presses the button it plays that particular song. I'm not looking to copy the file to my app, only want a shortcut to it in the users music library. It seems to me that the MPMediaItemPropertyAssetUrl is the way to go. If I'm correct the code for setting up the media picker looks like this:

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: {})

My confusion is in how do I add code that will return the users asset url so they can add it to a button to play?

Pacifico
  • 47
  • 10

1 Answers1

0

You would need to call the delegate function:

func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection)

You can then access the media items (whatever the user selected) via the MPMediaItemCollection:

for thisItem in mediaItemCollection.items as [MPMediaItem] {}

In that block at the end of the above line you can access the URL of selected item as so:

let itemUrl = thisItem.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL

Then play from the URL from your player

Cpt_JT
  • 1