我正在尝试在我的应用中保存iPod库中的音乐,以后可以使用它。我已经使用媒体选择器获取音乐URL,但我不知道如何将其保存在我的应用程序中并稍后播放。以下是我用来获取音乐网址的代码:
@IBAction func music(sender: AnyObject) {
var picker = MPMediaPickerController(mediaTypes: MPMediaType.Music)
picker.delegate = self
picker.allowsPickingMultipleItems = false
picker.prompt = NSLocalizedString("Select one music", comment: "")
self.presentViewController(picker, animated: false, completion: nil)
}
func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
var url = NSURL()
let items = mediaItemCollection.items
for item in items {
url = item.assetURL!
}
self.dismissViewControllerAnimated(true, completion: nil)
}
func mediaPickerDidCancel(mediaPicker: MPMediaPickerController) {
mediaPicker.dismissViewControllerAnimated(true, completion: nil)
}