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

6

4

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.

Probabilism answered 5/6, 2015 at 2:34 Comment(2)
You need to show your code.Julijulia
That's not enough code. You need to show whole methods, not just single lines. Show also, what you do when the user adds or deletes an item.Julijulia
J
5

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.

Julijulia answered 5/6, 2015 at 15:19 Comment(1)
but sadly it isn't, it seems that when you first associate the array with the player, no matter what happens to the array, the player will still play the first given array. The funny thing is, if the mediaPicker is called again and the same musics are chosen, it plays just fine. I was wondering why Apple does it, it just doesn't seem necessary. Anyway, I think I'll have to change my approach, thanks for the helpProbabilism
D
3

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

Demetra answered 20/4, 2018 at 0:12 Comment(0)
O
3

Using this code solved the problem for me:

player?.pause()                     
let playerItem = AVPlayerItem(asset: myAsset) 
player = nil 
player = AVPlayer()
player?.replaceCurrentItem(with: playerItem)

Instead of:

player?.pause()                     
player = nil 
player = AVPlayer()
player?.replaceCurrentItem(with: availablePlayerItem) 
//availablePlayerItem is a playerItem which has been created before and played once with this AVPlayer

Note: In both codes, the "player" is a global variable in my class which is:

var player : AVPlayer? = nil

Outthink answered 9/2, 2020 at 13:4 Comment(0)
B
2

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

Borghese answered 5/6, 2015 at 3:11 Comment(4)
yeah, I know that, but, if I try to add an item that has already been played, it gives the error, for exampleProbabilism
You need to check if you can insert before you do so, I add it to the answer I hope it helps youBorghese
I already knew that too, it's kinda of an unsolvable problem thanks to Apple. But thanks anywayProbabilism
Add a bit more of your code I am sure we will be able to help you if we can see a bit more of what you are doing.Borghese
C
1

It is required to check that the player item can be inserted into the player’s queue. Then add, remove or replace the player item based on the conditions as below:

if player.canInsert(playerItem, after: nil) == true {

            player = AVQueuePlayer(playerItem: playerItem)

        } else {

            player.remove(self.playerItem)

            player.replaceCurrentItem(with: playerItem)
}
Cachepot answered 11/1, 2020 at 10:4 Comment(0)
E
1

I initially initialized an AVPlayerItem inside a cell and added it to an AVPlayer (also inside the cell). I later presented a new vc and passed over that same cell's AVPlayerItem to a different AVPlayer inside the new vc and got a crash. To fix it I just made a copy of the playerItem using .copy() as? AVPlayerItem. A copy gives the new copy a different memory address from the original.

cell:

var cellPlayer: AVPlayer?
var cellsPlayerItem: AVPlayerItem?

cellsPlayerItem = AVPlayerItem(asset: AVAsset(url: url)) // cellsPlayerItem's memory address 0x1111111
cellPlayer = AVPlayer(playerItem: cellsPlayerItem!)

presented vc:

var vcPlayer: AVPlayer?

let playerItemCopy = cellsPlayerItem.copy() as? AVPlayerItem // playerItemCopy's memory address 0x2222222
let vcPlayer = AVPlayer(playerItem: playerItemCopy)
European answered 8/6, 2020 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.