In my quest to answer this question I came across this in a book.
"MPMediaItem, however, has an MPMediaItemPropertyAssetURL key (or assetURL property) whose value is a URL. That gives you a reference to the music file on disk,...having obtained an MPMediaItemās asset URL, you could use that URL to initialize an AVAudioPlayer, an AVPlayer, or an AVAsset."
So I tried to use the mediaPickerController to get the asset url like this.
func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
guard let mediaItem = mediaItemCollection.items.first else {
NSLog("No item selected.")
return
}
var songURL = mediaItem.valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL
print(songURL)
self.dismissViewControllerAnimated(true, completion: nil)
}
In the book it gives this as an example:
let arr = // array of MPMediaItem
let items = arr.map {
let url = $0.assetURL!
let asset = AVAsset(URL:url)
return AVPlayerItem(asset: asset)
}
self.qp = AVQueuePlayer(items:items)
self.qp.play()
Im not sure how to get the mediaItemCollection to the arr variable. The use of songURL and MediaItemCollection are inside the picker mediaPicker function. I can't access it outside of it's function. How do I get this to work?
OK. I figured this out.
The solution is to declare the variable outside of the function first, but under the class. I just created it like this:
Now I can access the songUrl outside of the function.