“一个AVPlayerItem不能与一个以上的AVPlayer实例相关联" [英] 'An AVPlayerItem cannot be associated with more than one instance of AVPlayer'

查看:91
本文介绍了“一个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.

所以我有一个包含AVPlayerItemsAVQueuePlayer的数组.首先,我将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实例相关联"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆