How to detect when AVPlayer video ends playing?

前端 未结 10 917
感动是毒
感动是毒 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:12

    Swift 4.2 Version:

    var player: AVPlayer!
      //
      //
    // Configure Player
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        let filepath: String? = Bundle.main.path(forResource: "selectedFileName", ofType: "mp4")
        if let filepath = filepath {
            let fileURL = URL.init(fileURLWithPath: filepath)
            player = AVPlayer(url: fileURL)
            let playerLayer = AVPlayerLayer(player: player)
            // Register for notification
            NotificationCenter.default.addObserver(self,
                                                   selector: #selector(playerItemDidReachEnd),
                                                             name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                                                             object: nil) // Add observer
    
            playerLayer.frame = self.view.bounds
            self.view.layer.addSublayer(playerLayer)
            player.play()
        }
    }
    // Notification Handling
    @objc func playerItemDidReachEnd(notification: NSNotification) {
        player.seek(to: CMTime.zero)
        player.play()
    }
    // Remove Observer
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    0 讨论(0)
  • 2020-11-28 08:13

    To get the AVPlayerItemDidPlayToEndTimeNotification your object needs to be an AVPlayerItem.

    To do so, just use the .currentItem property on your AVPlayer

    Now you will get a notification once the video ends!

    See my example:

    let videoPlayer = AVPlayer(URL: url)       
    
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:",
            name: AVPlayerItemDidPlayToEndTimeNotification, object: videoPlayer.currentItem)
    
    func playerDidFinishPlaying(note: NSNotification) {
        print("Video Finished")
    }
    

    Swift 3

    let videoPlayer = AVPlayer(URL: url)       
    
    NotificationCenter.default.addObserver(self, selector: Selector(("playerDidFinishPlaying:")), 
           name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: videoPlayer.currentItem)
    
    func playerDidFinishPlaying(note: NSNotification) {
        print("Video Finished")
    }
    

    Don't forget to remove the Observer in your deinit

    Swift 4, 5

    NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: .AVPlayerItemDidPlayToEndTime, object: nil)
    
    0 讨论(0)
  • 2020-11-28 08:14

    Swift 3.0

    let videoPlayer = AVPlayer(URL: url)
    NotificationCenter.default.addObserver(self, selector:#selector(self.playerDidFinishPlaying(note:)),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
    
    func playerDidFinishPlaying(note: NSNotification){
        //Called when player finished playing
    }
    
    0 讨论(0)
  • 2020-11-28 08:16

    Swift 4.0

    This one works for me. Thanks to @Channel

        private func playVideo(fileURL: String) {
    
                // Create RUL object
                let url = URL(string: fileURL)
    
                // Create Player Item object
                let playerItem: AVPlayerItem = AVPlayerItem(url: url!)
                // Assign Item to Player
                let player = AVPlayer(playerItem: playerItem)
    
                // Prepare AVPlayerViewController
                let videoPlayer = AVPlayerViewController()
                // Assign Video to AVPlayerViewController
                videoPlayer.player = player
    
                NotificationCenter.default.addObserver(self, selector: #selector(myViewController.finishVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
    
                // Present the AVPlayerViewController
                present(videoPlayer, animated: true, completion: {
                        // Play the Video
                        player.play()
                })
    
        }
    
        @objc func finishVideo()
        {
                print("Video Finished")
        }
    
    0 讨论(0)
  • 2020-11-28 08:18

    In Swift 3 and RxSwift 3.5 all you have to do is:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        NotificationCenter.default.rx.notification(Notification.Name.AVPlayerItemDidPlayToEndTime)
            .asObservable().subscribe(onNext: { [weak self] notification in
    
                //Your action
    
            }).addDisposableTo(disposeBag)
    }
    
    0 讨论(0)
  • 2020-11-28 08:27

    For SWIFT 3.0 This is working fine

    class PlayVideoViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
    
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(PlayVideoViewController.finishVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTimeNotification, object: nil)
        }
    
        func finishVideo()
        {
            print("Video Finished")
        }
    }
    
    0 讨论(0)
提交回复
热议问题