问题
I have seen many questions on this and none have had a solution. I am getting:
An AVPlayerItem can occupy only one position in a player's queue at a time
In the console after quickly taping a view button which should have one of two states:
- Successfully preloaded video
- only a videoURL which will then be loaded in another VC.
Why is this happening and how can I fix it?
This is my code which handels this in the transitioned to VC:
if numberMedia == 0 {
if selectedPost!.interimMedia[numberMedia].playerLayer != nil {
playCurrentMediaVid(currentMedia: selectedPost!.interimMedia[numberMedia])
} else {
let videoURL = selectedPost!.interimMedia[numberMedia].videoURL
if videoURL != nil {
//if we did not preload the video but have the cached vidURL
preloadVideo(media: selectedPost!.interimMedia[numberMedia])
playCurrentMediaVid(currentMedia: selectedPost!.interimMedia[numberMedia])
} else {
//if we dont have the cached vid url
selectedPost!.interimMedia[numberMedia].videoURL = getVideoURL(stringUrl: selectedPost!.interimMedia[numberMedia].videoURLString)
preloadVideo(media: selectedPost!.interimMedia[numberMedia])
playCurrentMediaVid(currentMedia: selectedPost!.interimMedia[numberMedia])
}
}
}
回答1:
I looked into this problem and found the following problem:
If you print the status.rawValue of the playerQueue and currentItem (the AVPlayerItem) of the AVPlayerQueue (sub-class of AVPlayer) you'll find that the status will be either 0 or 1. For reference: 0 means status.unknown and 1 means status.readyToPlay (the one you want). So if both prints are 0 it fails, which is why you get the error. The position is at unknown, yet you are trying to .play() the playerQueue, which of course results in the error.
My recommendation to you (the fix) is to either find out a way to make the loading of the media inside the AVPlayerItem faster (which may be done putting the URL in a AVAsset instead of trying to immediately init a URL into a AVPlayer), or can use KVO to detect when it has finished loading (sort of like a completionBlock-esque solution).
来源:https://stackoverflow.com/questions/56551087/an-avplayeritem-can-occupy-only-one-position-in-a-players-queue-at-a-time