Here is the thing, I have a MPMediaItemCollection with user choosen items(from the library). I used the mediaPicker to do that. Now, I need to get the URL from these items so I can play them on a AVPlayer. This is the best I can find, but when I "translate" to swift it gets messed up. If someone can help me I would appreciate a lot.
Asked
Active
Viewed 2,528 times
1 Answers
8
Here is your swift code:
func mediaPicker(mediaPicker: MPMediaPickerController!, didPickMediaItems mediaItemCollection: MPMediaItemCollection!) {
for thisItem in mediaItemCollection.items as! [MPMediaItem] {
let itemUrl = thisItem.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL
self.dismissViewControllerAnimated(true, completion: nil)
// Play the item using MPMusicPlayer
var appMusicPlayer = MPMusicPlayerController.applicationMusicPlayer()
appMusicPlayer.play()
// Play the item using AVPlayer
let playerItem = AVPlayerItem(URL: itemUrl)
let player = AVPlayer(playerItem: playerItem)
player.play()
}
}

Dharmesh Kheni
- 71,228
- 33
- 160
- 165
-
Thanks you so much, it worked perfectly, but I have a doubt. When more than one music is chosen it only plays the last one. Do you know how to change it? – Gabriel Bitencourt May 30 '15 at 13:56
-
You just need to get the first item mediaItemCollection.items.first as! MPMediaItem – Leo Dabus Jun 03 '15 at 01:21