“一个AVPlayerItem不能与一个以上的AVPlayer实例相关联"

编程入门 行业动态 更新时间:2024-10-16 16:13:29
本文介绍了“一个AVPlayerItem不能与一个以上的AVPlayer实例相关联"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道这个问题已经遍及整个Stack Overflow,但是,大多数问题都是古老的,与我在这里要问的问题无关.

I know this question is all over Stack Overflow but still, most of them are old and not related with what I'm going to ask here.

所以我有一个包含AVPlayerItems和AVQueuePlayer的数组.首先,我将AVQueuePlayer设置为播放数组项:

So I've got an array with AVPlayerItems and an AVQueuePlayer. At first I set the AVQueuePlayer to play the array items:

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) }

但是,例如,当用户添加一个新项目或删除一个项目时,我尝试更新播放器(与最初设置它的方式相同),但它给了我错误.我想知道是否有解决方法或任何解决方案.用户删除歌曲的方式如下:

But, when the user, for example, adds a new Item or delete one, I try updating the player (the same way I set it in the first place), but it gives me the error. I want to know if there is any way around of it, or any solution. This is how the user deletes a song:

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 } }

我为此而自杀,所以,如果有人可以帮助我,我将不胜感激.

I'm killing myself over this, so, if someone can help me, I would appreciate it.

推荐答案

问题是,当您用此行更新项目数组时,您正在创建一个新的播放器对象(在tableView:commitEditingStyle:forRowAtIndexPath:中),

The problem is that you're creating a new player object when you update the array of items with this line (in tableView:commitEditingStyle:forRowAtIndexPath:),

audioPlayer = AVQueuePlayer(items: mediaArray)

执行此操作时,似乎播放器项目仍与原始播放器相关联.您应该能够删除该行,以使其正常工作.从mediaArray插入或删除项目应该足以完成您想要的操作;您无需创建新的播放器.

It seems that the player items are still associated with the original player when you do this. You should be able to delete that line, to make it work properly. Inserting or deleting items from mediaArray should be enough to do what you want; you don't need to create a new player.

更多推荐

“一个AVPlayerItem不能与一个以上的AVPlayer实例相关联"

本文发布于:2023-11-28 14:12:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:相关联   能与   实例   AVPlayerItem   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!