How to detect when AVPlayer video ends playing?

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

    2019

    It's really this simple

        NotificationCenter.default.addObserver(
            self,
            selector: #selector(fileComplete),
            name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
            object: nil)
    

    (It's fine for the object to be nil.)

    and then

    @objc func fileComplete() {
        print("IT'S DONE!")
    }
    

提交回复
热议问题