AVPlayer inside of UICollectionViewCell stops playing when app enters background

我们两清 提交于 2019-12-23 18:51:11

问题


I have a local video file playing in a loop using AVPlayerLooper but whenever I press the home button (app enters background) and then reopen the app, the video has stopped playing. I also have an AVPlayerLooper inside of a simple view and not a collectionViewCell. To solve the issue on this view I simply call player.play() inside of applicationDidBecomeActive(). This does not work for the collectionViewCell.

Have tried removing playerLayer before the app enters the background and adding it again when the app is active without success.

VideoView setup code:

func setupPlayerView() {

        if let videoURL = allExercises.array[currentExercise].videoLink {
            if player == nil {
            player = AVQueuePlayer()
            player?.isMuted = true
             playerLayer = AVPlayerLayer(player: player)
                self.layer.addSublayer(playerLayer)
                playerLayer.frame = self.frame
                playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
                }

            theItem = AVPlayerItem(url: URL(fileURLWithPath: videoURL))
            playerLooper = AVPlayerLooper(player: player!, templateItem: theItem!)


            player?.play()
            print("DIDSETUP")
    }

        }

collectionView cellForItemAt:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        currentExercise = indexPath.section

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LiveView", for: indexPath) as! HorizontalLiveViewCell

        cell.nameLabel.text = allExercises.array[indexPath.section].name

        return cell

    }

VideoView variable of the collectionViewCell:

var videoView: VideoPlayerView {

        let video = VideoPlayerView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width * (9 / 16)))

        return video
    }

Everything works perfectly until I click the home button sending the app into the background. How can I restart the video? The whole cell can be reloaded that would be no problem.

来源:https://stackoverflow.com/questions/58241061/avplayer-inside-of-uicollectionviewcell-stops-playing-when-app-enters-background

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