
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How to use Swift to detect when AVPlayer video ends playing?
To detect the end of a video in swift we’ll need to create a video player, then use notifications to detect when the video stops playing. We’ll do this with help of an example in swift.
Let’s create a project and drag and drop any video with extension “mp4”, select copy resource if required and add to the target of our project.
Now we’ll programmatically first create a Video Player, then we’ll create url of the video in our project, and then we’ll play the video.
var videoPlayer: AVPlayer!
Now, in the viewDidAppear add the following code.
override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) let filepath: String? = Bundle.main.path(forResource: "Introduction", ofType: "mp4") if let videoPath = filepath { let url = URL.init(fileURLWithPath: videoPath) videoPlayer = AVPlayer(url: url) let playerLayer = AVPlayerLayer(player: videoPlayer) playerLayer.frame = self.view.bounds self.view.layer.addSublayer(playerLayer) videoPlayer.play() } }
The above code will create a video player, and add it as a sublayer to our default view in the view controller.
Now we need to add an observer to our player, and also create an observer method that should be called when it detects the end of the video. Add the code block below to the viewDidAppear method.
NotificationCenter.default.addObserver(self, selector: #selector(videoDidEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
Now we need to write the videoDidEnd method too, Inside this method you can perform any action you want, for now, we’ll just print “video ended” when the video ends and the notification center calls this method.
@objc func videoDidEnd(notification: NSNotification) { print("video ended") }
When we run this code we get the following result.
Once the video stops playing we get output in our console.
Now, the above code does the required thing for us, but for efficient memory usage, we also need to remove the observer, which can be done using the code below.
deinit { NotificationCenter.default.removeObserver(self) }
- Related Articles
- How to change video playing speed using JavaScript?
- How to detect shake gesture using Swift?
- How to use YouTube Video Editor
- HTML5 video not playing in Firefox
- How to detect swipe vertically on a ScrollView using Swift?
- How to Use YouTube Analytics for Video Optimization?
- How to use MBProgressHUD with swift?
- How to use UICollectionView in Swift?
- How To Use Technical SEO To Optimize For Video Carousels
- Playing MP4 video in WebView using HTML5 in Android
- How to use the front camera in Swift?
- How to use a Background Thread in Swift?
- How to detect when an OptionMenu or Checkbutton changes in Tkinter?
- How to use research tab for new video topics in YouTube
- Where and how to use static variable in swift?
