How to detect when AVPlayer video ends playing?

前端 未结 10 922
感动是毒
感动是毒 2020-11-28 07:48

I\'am using AVPlayer for playing local video file (mp4) in Swift. Does anyone know how to detect when video finish with playing? Thanks

10条回答
  •  感情败类
    2020-11-28 08:38

    For SWIFT 3.0

    Here 'fullUrl' is the URL of the video and make sure that there would be no space in the URL, You should replace 'Space' with '%20' so that URL will work file.

      let videoURL = NSURL(string: fullUrl)
      let player = AVPlayer(url: videoURL! as URL)
    
      playerViewController.delegate = self
      playerViewController.player = player
      self.present(playerViewController, animated: false) {
    
        self.playerViewController.player!.play()
    
        NotificationCenter.default.addObserver(self, selector: #selector(yourViewControllerName.playerDidFinishPlaying), name: Notification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
      }
    

    Add this below given method in your view controller.

    func playerDidFinishPlaying(){    
    print("Video Finished playing in style")
    }
    

提交回复
热议问题