这就是事情,我有一个MPMediaItemCollection,用户选择项目(来自库)。我使用mediaPicker来做到这一点。现在,我需要从这些项目中获取URL,以便我可以在AVPlayer上播放它们。 This is the best I can find,但是当我"翻译"迅速陷入困境。如果有人可以帮助我,我会非常感激。
答案 0 :(得分:7)
这是你的快捷代码:
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()
}
}