Gab*_*urt 4 ios avqueueplayer swift avplayeritem
我知道这个问题遍布 Stack Overflow,但其中大多数都是旧问题,与我要在这里问的问题无关。
所以我有一个带有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)
}
Run Code Online (Sandbox Code Playgroud)
但是,例如,当用户添加新项目或删除项目时,我尝试更新播放器(与我首先设置它的方式相同),但它给了我错误。我想知道是否有任何解决办法,或者有什么解决方案。这是用户删除歌曲的方式:
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
}
}
Run Code Online (Sandbox Code Playgroud)
我为此自杀了,所以,如果有人能帮助我,我将不胜感激。
问题是,当您使用此行更新项目数组时,您正在创建一个新的玩家对象(在 tableView:commitEditingStyle:forRowAtIndexPath: 中),
audioPlayer = AVQueuePlayer(items: mediaArray)
Run Code Online (Sandbox Code Playgroud)
当您执行此操作时,播放器项目似乎仍然与原始播放器相关联。您应该能够删除该行,以使其正常工作。从 mediaArray 中插入或删除项目应该足以满足您的要求;你不需要创建一个新的播放器。
| 归档时间: |
|
| 查看次数: |
8212 次 |
| 最近记录: |