Check play state of AVPlayer
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.'
Related reading
- Checking a null value in Objective-C that has been returned from a JSON string
- Checking for multiple asynchronous responses from Alamofire and Swift
- Checking if an Android application is running in the background
- Checking if an object is a given type in Swift
- Circular UIImageView in UITableView without performance hit?
- clang error SDK does not contain 'libarclite' at the path
- Class AMSupportURLConnectionDelegate is implemented in both
- Class does not conform NSObjectProtocol
.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.