我希望能够从我的音乐库中选择两首歌曲,并在选择歌曲后解除选择器。现在我必须按完成按钮才能将其关闭。我怎么能这样做?
//MEDIAPLAYER---------------------------------------------------------------
func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
if mediaItemCollection.items.count > 1 {
let aMediaItem = mediaItemCollection.items[0] as MPMediaItem
music = aMediaItem
NSLog("\(aMediaItem.title)selected")
let url: NSURL = (music.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL)!
do {
musicPlayer = try AVAudioPlayer(contentsOfURL: url)
} catch {
return
}
}
if mediaItemCollection.items.count > 1 {
let aMediaItem2 = mediaItemCollection.items[1] as MPMediaItem
music2 = aMediaItem2
NSLog("\(aMediaItem2.title)selected")
let url2: NSURL = (music2.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL)!
do {
musicPlayer2 = try AVAudioPlayer(contentsOfURL: url2)
} catch {
return
}
}
}
答案 0 :(得分:1)
更新:MPMediaPickerController不提供任何默认方法,您可以根据需要直接关闭控制器
要在歌曲选择中进行自定义,您可以使用MPMediaQuery
和MPMediaItem
,它会返回包含详细信息的歌曲列表。您可以在UITableView / UICollectionView中显示它并实现自定义逻辑
旧代码: 只有在手动按下完成后要进行某些处理时才能使用此功能
func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
//rest of your code
if mediaItemCollection.items.count == 2 {
self.dismissViewControllerAnimated(true, completion:nil)
}
//rest of your code
}