title: UICollectionView Video Performance
body: <p>I have a UICollectionView for Apple TV. There are at most 6 cells all displaying on the same screen. Each cell has a different looping video playing. However, I find that anymore than 2 videos causing very choppy video playback. How can I improve performance of the playing videos?</p>
  
  <pre><code>class ExerciseCell: UICollectionViewCell {
  
  @IBOutlet private weak var stationNumberLabel: UILabel!
  @IBOutlet private weak var exerciseNameLabel: UILabel!
  
  private var looperPlayer:AVPlayerLooper!
  
  var exercise:ExerciseData! {
      didSet {
          self.stationNumberLabel.text = exercise.stationNumber
          self.exerciseNameLabel.text = exercise.exerciseName
          playVideo()
      }
  }
  
  private func playVideo()
  {
      //if let videoURL = URL(string:self.exercise.videoURL)
      if let videoPath = Bundle.main.path(forResource: self.exercise.videoName, ofType: "mp4")
      {
          //let playerItem = AVPlayerItem(url: videoURL)
          let playerItem = AVPlayerItem(url: URL(fileURLWithPath: videoPath))
          let queuePlayer = AVQueuePlayer(playerItem: playerItem)
          let playerLayer = AVPlayerLayer(player: queuePlayer)
  
          playerLayer.frame = self.layer.bounds
          playerLayer.videoGravity = .resize
          playerLayer.player = queuePlayer
  
          self.looperPlayer = AVPlayerLooper(player: queuePlayer, templateItem: playerItem)
          self.layer.insertSublayer(playerLayer, at: 0)
  
          queuePlayer.volume = 0.0
          queuePlayer.play()
      }
      else
      {
          self.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
      }
  }}
  </code></pre>
  
  <p>The above is called from the <code>UICollectionViewController</code></p>
  
  <pre><code>override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell
  {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ExerciseCell
  
      cell.exercise = self.exercisesArray[indexPath.row]
  
      return cell
  }
  </code></pre>