How to detect when AVPlayer video ends playing?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Detecting when an AVPlayer video finishes playing requires observing the .AVPlayerItemDidPlayToEndTime notification on the AVPlayerItem. This notification fires when the player reaches the end of the item's duration. Alternative approaches include using Key-Value Observing (KVO) on the player item's status property and adding a boundary time observer. For looping video, seek back to the beginning in the notification handler. This article covers all approaches in both UIKit and SwiftUI.
Using NotificationCenter
Pass the playerItem as the object parameter to receive notifications only for that specific item. Passing nil receives notifications from all player items.
Using Closure-Based Observer
The closure-based API returns a token that must be retained and removed in deinit. Use [weak self] to avoid retain cycles.
Looping Video (Replay on End)
AVPlayerLooper provides gapless looping without manual seek. For simple loop-on-end, seeking to .zero in the notification handler works but may have a brief gap.
Detecting Playback Failure
.AVPlayerItemFailedToPlayToEndTime fires when playback is interrupted by an error (network failure, corrupted file). Always observe both notifications for robust error handling.
Boundary Time Observer
Boundary time observers fire when playback passes specific timestamps. Use this for pre-end actions like showing UI elements before the video finishes.
SwiftUI Integration
Periodic Time Observer (Progress Tracking)
Periodic time observers provide continuous progress updates. Use them for progress bars, time labels, and detecting when playback is near the end.
Common Pitfalls
- Not removing observers in deinit: Failing to remove notification observers causes crashes if the notification fires after the view controller is deallocated. Always remove in
deinitor use[weak self]with closure observers. - Passing nil for the notification object:
addObserver(... object: nil)receives notifications from allAVPlayerIteminstances. If your app has multiple players, you receive duplicate callbacks. Always pass the specificplayerItem. - Notification not firing for streaming content: If a stream has no defined end (live stream),
.AVPlayerItemDidPlayToEndTimenever fires. CheckplayerItem.duration— live streams have.indefiniteduration. - Seek to zero not working for loop:
player.seek(to: .zero)is asynchronous. If you callplayer.play()before the seek completes, playback may stutter. Use the completion handler:player.seek(to: .zero) { _ in player.play() }. - Ignoring AVPlayerItemFailedToPlayToEndTime: Only observing the success notification misses playback errors. Always observe both
.AVPlayerItemDidPlayToEndTimeand.AVPlayerItemFailedToPlayToEndTime.
Summary
- Observe
.AVPlayerItemDidPlayToEndTimenotification to detect when a video finishes - Always pass the specific
playerItemas the observation object, not nil - Use
AVPlayerLooperfor seamless gapless looping on iOS 10+ - Use boundary time observers to trigger actions at specific timestamps before the end
- Remove all observers in
deinitto prevent crashes - Observe
.AVPlayerItemFailedToPlayToEndTimefor error handling
Related reading
- How to detect when keyboard is shown and hidden
- How to determine height of UICollectionView with FlowLayout
- How to determine if an NSDate is today?
- How to determine MIME type of file in android?
- How to determine the content size of a UIWebView?
- How to determine the content size of a WKWebView?
- How to determine the screen width in terms of dp or dip at runtime in Android?
- How to determine when Fragment becomes visible in ViewPager
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.