title: Swift iOS -AVPlayer Video Freezes / Pauses When App Comes Back from Background
body: <p>I have a video playing in a loop on the login page of my app. I followed this Youtube tutorial to get it to work <a href="https://youtu.be/L5j5kQEWN7o" rel="noreferrer">loop video in view controller</a></p>
  
  <p>The problem is when the app goes to the background, if I don't come back right away, when i do come back the video gets frozen.</p>
  
  <p>According to the <a href="https://developer.apple.com/library/content/qa/qa1668/_index.html#//apple_ref/doc/uid/DTS40010209-CH1-VIDEO" rel="noreferrer">Apple Docs</a> that's supposed to happen.</p>
  
  <p>I tried to use the NotificationCenter's <code>Notification.Name.UIApplicationWillResignActive</code> but that didn't work. </p>
  
  <p><strong>How do I get the video to keep playing once the app returns from the background?</strong></p>
  
  <pre><code>var player: AVPlayer!
  var playerLayer: AVPlayerLayer!
  
  override func viewDidLoad() {
          super.viewDidLoad()
  
          configurePlayer()
  }
  
  
  @objc fileprivate func configurePlayer(){
  
          let url = Bundle.main.url(forResource: "myVideo", withExtension: ".mov")
  
          player = AVPlayer.init(url: url!)
          playerLayer = AVPlayerLayer(player: player!)
          playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
          playerLayer.frame = view.layer.frame
  
  
          player.actionAtItemEnd = AVPlayerActionAtItemEnd.none
  
          player.play()
  
          view.layer.insertSublayer(playerLayer, at: 0)
  
          NotificationCenter.default.addObserver(self, selector: #selector(playerItemReachedEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
  
          NotificationCenter.default.addObserver(self, selector: #selector(playerItemReachedEnd), name: Notification.Name.UIApplicationWillResignActive, object: player.currentItem)
  
      }
  
  @objc fileprivate func playerItemReachedEnd(){
          player.seek(to: kCMTimeZero)
      }
  </code></pre>