An AVPlayerItem can occupy only one position in a player's queue at a time?

旧城冷巷雨未停 提交于 2019-12-13 08:29:36

问题


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:

  1. Successfully preloaded video
  2. 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

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