No AVPlayer Delegate? How to track when song finished playing? Objective C iPhone development
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
AVPlayer does not provide a direct delegate callback for playback completion, unlike AVAudioPlayer. The correct completion mechanism is observing AVPlayerItemDidPlayToEndTimeNotification on the current player item. Reliable behavior also requires observer cleanup and handling item replacement.
Completion Tracking with Notification Center
Register for end-of-playback notification on the active AVPlayerItem.
This is the standard replacement for delegate-style completion.
Remove Observers Correctly
If you change tracks and forget observer cleanup, callbacks may fire multiple times or for stale items.
Always remove observers in dealloc for safety.
Handle Failure and Interruptions
Playback completion is only one terminal state. Also observe failure notifications and audio session interruptions.
Handling these states improves user experience during network and session disruptions.
Looping and Playlist Progression
In completion callback, decide whether to loop or advance playlist.
Loop current track:
Playlist progression should replace item and update index atomically to avoid race conditions.
Progress Updates Are Separate
addPeriodicTimeObserverForInterval is useful for progress UI, not completion detection.
Remove this token when no longer needed.
Background Audio and Session Configuration
If your app should keep playing while backgrounded, configure audio session category before creating player objects. Without this setup, playback can pause when app state changes, and completion callback behavior may look inconsistent during testing.
Also enable the Background Modes capability for audio in project settings. Session setup and notification handling should be treated as one system, because both affect end-of-track behavior users observe.
Prefer Main Queue for UI Side Effects
Completion callback may trigger UI updates such as resetting play button state or moving to next row in a playlist table. Keep UI mutations on main queue to avoid race conditions.
For pure playback engine state, background queues can be fine, but UI work should stay on main thread.
Testing Playback Completion
A practical test plan:
- play short local file and assert one completion callback
- replace item mid-play and assert no stale completion callback
- simulate failure path and confirm error handling
Playback code is stateful, so deterministic tests prevent regressions during refactor.
Common Pitfalls
- Expecting
AVPlayerdelegate callback for completion. - Observing completion without scoping notification object to current item.
- Forgetting observer removal on item replacement.
- Treating periodic time observer as completion event.
- Handling completion only and ignoring failure or interruption states.
Summary
- Use
AVPlayerItemDidPlayToEndTimeNotificationfor completion tracking. - Scope observers to active player item and clean up on replacement.
- Handle failures and interruptions, not only successful completion.
- Keep progress updates and completion logic as separate concerns.
- Add deterministic tests for item lifecycle and callback behavior.

