我试图将歌曲的标题,作者和歌曲的播放秒数分配给一组变量。但是我无法从歌曲中获取任何信息,这是目前我崩溃的最好方法。

func presentPicker (sender:AnyObject) {
//I have all of this within an IBAction if that matters, I am new to programming in general so sorry if theres any stupid mistakes
    let mediaPicker = MPMediaPickerController(mediaTypes: .Music)
    mediaPicker.delegate = self
    mediaPicker.allowsPickingMultipleItems = false
    presentViewController(mediaPicker, animated: true, completion: {println(MPMediaItemCollection())})

}

最佳答案

您不应该使用completionpresentViewController:参数。当控制器成功显示时,完成运行,但是您要在结束播放后抓取歌曲。您需要在您的类中实现此委托方法:

func mediaPicker(mediaPicker: MPMediaPickerController!,
    didPickMediaItems mediaItemCollection: MPMediaItemCollection!) {
    println(mediaItemCollection)
}


当用户选择一首歌曲时,该类将在您的班级上调用,因为您将delegate设置为等于self。您可能还想实现这一点,以了解他们是否取消了选择器:

func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController!)

10-08 05:59