its working me for #iOS
version 10 and higher
# declare your player and
var player = AVPlayer()
var playerVC = AVPlayerViewController()
let item = AVPlayerItem(url: module.fileURL!)
DispatchQueue.main.async(execute: {
self.playerVC.view.backgroundColor = UIColor.clear
self.player.replaceCurrentItem(with: item)
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
self.playerVC.player = self.player
self.player.play()
self.playerVC.showsPlaybackControls = true
if #available(iOS 11.0, *) {
self.playerVC.exitsFullScreenWhenPlaybackEnds = true
}
self.playerVC.view.frame = self.videoView.bounds
NotificationCenter.default.addObserver(self, selector: #selector(self.playerItemDidReachEnd(notification:)), name: .AVPlayerItemDidPlayToEndTime, object:self.playerVC.player!.currentItem)
self.videoView.addSubview((self.playerVC.view)!)
})
Listen to the end of your video file
@objc func playerItemDidReachEnd(notification: Notification?) {
print("Finishing")
dismiss(animated: true, completion: nil)
}