Resume AVPlayer video playback after app become active

后端 未结 5 1309
天涯浪人
天涯浪人 2021-02-02 15:53

I write custom player from AVPlayer for video playback. According to Apple docs set the video layer:

    self.player = [IPLPlayer new];
    self.player.playerLa         


        
5条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 16:33

    This works for me on Swift 3

    Add somewhere while setting up the view:

    NotificationCenter.default.addObserver(self, 
      selector: #selector(appWillEnterForegroundNotification),
      name: .UIApplicationWillEnterForeground, object: nil)
    

    Grab your player and force it to play:

    func appWillEnterForegroundNotification() {
      myPlayer.play()
    }
    

    Don't forget to remove the observers when you don't need them:

    override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)
      NotificationCenter.default.removeObserver(self)
    }
    

提交回复
热议问题