我有一个包含AVPlayerItems的数组和一个AVQueuePlayer。一开始我设置AVQueuePlayer播放数组中的项目。
func mediaPicker(mediaPicker: MPMediaPickerController!, didPickMediaItems mediaItemCollection: MPMediaItemCollection!) {
mediaItems = mediaItemCollection.items
for thisItem in mediaItems as [MPMediaItem] {
var itemUrl = thisItem.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL
var playerItem = AVPlayerItem(URL: itemUrl)
mediaArray.append(playerItem)
}
updateMetadata()
audioPlayer = AVQueuePlayer(items: mediaArray)
self.dismissViewControllerAnimated(true, completion: nil)
}
然而,当用户添加新项目或删除一个项目时,我尝试更新播放器(与一开始设置的方式相同),但是却出现了错误。我想知道是否有任何解决方法或解决方案。以下是用户删除歌曲的方式:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete{
mediaArray.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
audioPlayer = AVQueuePlayer(items: mediaArray) //here gives the error
}
}
我为此感到苦恼,如果有人能帮我,我将不胜感激。