title: Trying to pause video when the UICollectionViewCell is out of view
body: <p>I'm populating a collection view with URLs loaded from Firebase. But I can't get the video to pause when I exit the view or scroll the collection view up. When I go back using the navigation controller, I can still hear the video playing in the background. Then when I enter the view again, the video starts playing, but the first one never finished.</p>
  
  <p>This is my view controller. Any tips? thank you! I'm still new to Swift, so please excuse any ignorance on my end.</p>
  
  <pre><code>import UIKit
  import AVKit
  import AVFoundation
  import Firebase
  import FirebaseDatabase
  import SDWebImage
  
  class ComedyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
  
      var avPlayer = AVPlayer()
      var avPlayerLayer = AVPlayerLayer()
  
      @IBOutlet weak var comedyCollectionView: UICollectionView!
  
      var comedyVideoArray = [ComedyModel]()
  
      var comedyDBRef: DatabaseReference! {
          return Database.database().reference().child("comedy")
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          loadComedyDB()
      }
  
  
      func loadComedyDB() {
          comedyDBRef.observe(DataEventType.value, with: { (snapshot) in
  
              if snapshot.childrenCount &gt; 0 {
                  self.comedyVideoArray.removeAll()
  
                  for comedyData in snapshot.children.allObjects as! [DataSnapshot] {
                      let comedyObject = comedyData.value as? [String: AnyObject]
                      let comedyPostTitle = comedyObject?["title"]
                      let comedyPostDescription = comedyObject?["description"]
                      let comedyArticleLink = comedyObject?["link"]
                      let comedyVideoUrl = comedyObject?["url"]
                      let allComedyData = ComedyModel(title: comedyPostTitle as! String?, description: comedyPostDescription as! String?, link: comedyArticleLink as! String?, url: comedyVideoUrl as! String?)
  
                      self.comedyVideoArray.append(allComedyData)
  
                      self.comedyCollectionView.reloadData()
                  }
              }
          })
      }
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          print(comedyVideoArray.count)
          return comedyVideoArray.count
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
          let cellB = comedyCollectionView.dequeueReusableCell(withReuseIdentifier: "comedyCell", for: indexPath) as! ComedyCollectionViewCell
          let data = comedyVideoArray[indexPath.row]
          let item = AVPlayerItem(url: URL(string: data.url!)!)
  
          self.avPlayer = AVPlayer(playerItem: item)
          self.avPlayer.actionAtItemEnd = .none
  
          self.avPlayerLayer = AVPlayerLayer(player: self.avPlayer)
          self.avPlayerLayer.videoGravity = .resizeAspectFill
          self.avPlayerLayer.frame = CGRect(x: 0, y: 0, width: cellB.frame.size.width, height: cellB.frame.size.height / 2)
  
          cellB.videoView.layer.addSublayer(self.avPlayerLayer)
  
          self.avPlayer.play()
          //cellB.videoView.sd_setImage(with: URL(string: data.link!), placeholderImage: UIImage(named: "1"))
  
          return cellB
      }
  
      func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  
          if collectionView == self.comedyCollectionView {
              self.avPlayer.pause()
          }
      }
  }
  </code></pre>