Check play state of AVPlayer
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Checking AVPlayer state is more nuanced than reading one property. Real playback behavior involves playing, pausing, waiting for buffering, end-of-item completion, and failure states. A robust solution combines timeControlStatus, rate, and item-level events so the UI reflects what the player is actually doing.
Start with timeControlStatus
For most user-facing playback state, timeControlStatus is the best high-level signal.
This is more accurate than checking only rate, because waiting and buffering are not the same thing as an intentional pause.
Observe Player State Changes
Playback state changes over time, so observation is often more useful than one-off inspection.
This helps keep play buttons, spinners, and overlays synchronized with the actual player state.
Handle Completion and Failure Too
A player can stop because the item finished or because the item failed. Those are different states and should be handled differently.
Without completion and failure handling, playback UI often gets stuck in an incorrect state.
Build a Small State Model
A small enum can keep the rest of the app from scattering playback logic across many files.
This gives the rest of the app one consistent source of truth.
One useful UI pattern is to map these playback states into a view-model layer rather than letting every button and overlay inspect AVPlayer directly. That keeps state transitions consistent and makes UI tests much easier to reason about.
Common Pitfalls
- Using only
rateto decide whether playback is active. - Ignoring
.waitingToPlayAtSpecifiedRateand reporting buffering as paused. - Forgetting to observe item failure and completion separately from player status.
- Leaving observers alive after a view or player is gone.
- Spreading playback-state rules across many UI classes instead of centralizing them.
Summary
- Use
timeControlStatusas the primary AVPlayer state signal. - Observe player properties instead of relying only on one-time checks.
- Handle completion and failure explicitly at the item level.
- A small playback-state model makes the UI more consistent.
- '
rateis useful, but by itself it is not a complete play-state answer.'

