'An AVPlayerItem cannot be associated with more than one instance of AVPlayer'

守給你的承諾、 提交于 2019-12-02 05:12:02

问题


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.

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.


回答1:


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)

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.




回答2:


The way I solved this issue is that I kept a reference to AVAsset objects in an array and then mapped it to [AVPlayerItem] (the type that AVQUeuePlayer init accepts).

To elaborate, we needed to make an audio recorder (code on github) that can be paused, so I used an AVQueuePlayer, but ran into the same issues as the OP, and the only clean solution is to recreate this player object with a new array.

The relevant code in Swift 4.1:

class RecordViewController: UIViewController {

    var audioRecorder: AVAudioRecorder?

    var audioChunks = [AVURLAsset]()
    var queuePlayer:  AVQueuePlayer?

// irrelevant code omitted

    func stopRecorder() {
        self.audioRecorder?.stop()
        let assetURL  = self.audioRecorder!.url

        self.audioRecorder = nil

        let asset = AVURLAsset(url: assetURL)
        self.audioChunks.append(asset)

        let assetKeys = ["playable"]
        let playerItems = self.audioChunks.map {
            AVPlayerItem(asset: $0, automaticallyLoadedAssetKeys: assetKeys)
        }

        self.queuePlayer = AVQueuePlayer(items: playerItems)
        self.queuePlayer?.actionAtItemEnd = .advance
    }

    func startPlayer() {
        self.queuePlayer?.play()
    }

    func stopPlayer() {
        self.queuePlayer?.pause()
        self.queuePlayer = nil
    }
}

(In the end, AVAssetExportSession is used to stitch the chunks of audio back together, so storing AVAssets insead of AVPlayerItems was even more beneficial in the end.)




回答3:


From Apple documentation:

Before add an item:

canInsertItem(_:afterItem:)

Returns a Boolean value that indicates whether a given player item can be inserted into the player’s queue.

func canInsertItem(_ item: AVPlayerItem!, afterItem afterItem: AVPlayerItem!) -> Bool

Adding an Item:

insertItem(_:afterItem:)

Places given player item after a specified item in the queue.

 func insertItem(_ item: AVPlayerItem!, afterItem afterItem: AVPlayerItem!)

Removing an Item:

removeItem(_:)

Removes a given player item from the queue.

func removeItem(_ item: AVPlayerItem!)



来源:https://stackoverflow.com/questions/30657462/an-avplayeritem-cannot-be-associated-with-more-than-one-instance-of-avplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!