title: AVPlayer video still playing after segue to next view controller
body: <p>I'm currently looping my mp4 video with no playback controls, kind of like a gif but with sound. But I do not know why when I segue to the next view controller, the video is still playing. Does anybody know the simplest method to resolve this issue?</p>
  
  <p>ViewController</p>
  
  <pre><code>import UIKit
  import AVKit
  import AVFoundation
  fileprivate var playerObserver: Any?
  
  
  class ScoreController: UIViewController {
      @IBOutlet var label: UILabel!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
  
          let returnValue: Int = UserDefaults.standard.integer(forKey: "userScore")
          label.text = "Your Score: \(returnValue)/30"
  
          do {
              try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
              let path = Bundle.main.path(forResource: "Innie-Kiss", ofType:"mp4")
              let player = AVPlayer(url: URL(fileURLWithPath: path!))
              let resetPlayer = {
                  player.seek(to: kCMTimeZero)
                  player.play()
              }
              playerObserver = NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil) { notification in resetPlayer() }
              let controller = AVPlayerViewController()
              controller.player = player
              controller.showsPlaybackControls = false
              self.addChildViewController(controller)
              let screenSize = UIScreen.main.bounds.size
              let videoFrame = CGRect(x: 0, y: 130, width: screenSize.width, height: (screenSize.height - 130) / 2)
              controller.view.frame = videoFrame
              self.view.addSubview(controller.view)
              player.play()
          } catch {
          }
      }
  
      override func viewDidDisappear(_ animated: Bool) {
          super.viewDidDisappear(animated)
      }
      fileprivate var player: AVPlayer? {
          didSet { player?.play() }
      }
      deinit {
          guard let observer = playerObserver else { return }
          NotificationCenter.default.removeObserver(observer)
      }
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()  
      }
      @IBAction func doneButton(_ sender: Any) {
          self.performSegue(withIdentifier: "done", sender: self)
      }
  }
  </code></pre>