AVPlayer inside of UICollectionViewCell stops playing when app enters background

Viewed 416

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.

0 Answers
Related