How to play video with AVPlayerViewController AVKit in Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
AVPlayerViewController is the default high-quality video UI in iOS because it handles playback controls, fullscreen transitions, and many system media behaviors automatically. The API is small, but production integration requires deliberate lifecycle, buffering, and error handling. This guide focuses on practical Swift patterns that stay stable in real apps.
Quick Modal Playback Pattern
The most direct approach is presenting AVPlayerViewController modally and starting playback once presented.
Keep a strong reference to player when you need later controls such as pause or replay.
Embedded Playback with Child Controller
Use child containment when video should appear inside a larger layout instead of fullscreen modal.
This pattern works well for feed cards, course screens, and article detail pages.
Observe Playback Completion
For replay UI or analytics, observe end-of-item notifications.
Observer cleanup is essential to avoid duplicate callback behavior after navigation.
Handle Status and Recovery
Check status before assuming playback problems are network-related.
Expose user-visible retry actions instead of silent failure loops.
Picture in Picture and UX Controls
On supported versions, configure picture-in-picture behavior for better multitasking.
Also consider:
- pausing when screen disappears if background playback is not intended.
- disabling repeated play taps while presentation animation runs.
- preserving playback position for resume flows.
These details improve usability more than UI cosmetics.
Interruptions and App Lifecycle Handling
Playback can pause due to phone calls, Siri, or app background transitions. Handle interruption notifications so UI state stays accurate.
Also decide whether playback should resume automatically or wait for user confirmation after interruptions.
Diagnostics for Support and QA
If users report playback failures, collect actionable context:
- URL source type, local or remote.
- item status and error domain.
- device model and iOS version.
- network conditions for streaming cases.
Structured diagnostics reduce guesswork and speed up incident resolution.
Common Pitfalls
- Presenting player before URL validation and then showing blank controls.
- Not retaining player when later interaction requires state access.
- Forgetting observer cleanup, causing duplicated completion events.
- Ignoring item status and treating every failure as connectivity issue.
- Testing only simulator and missing device-specific audio behaviors.
Summary
AVPlayerViewControlleris the preferred iOS video UI for most use cases.- Modal presentation is fastest; child containment is best for inline layouts.
- Add status checks, completion observation, and replay handling.
- Configure picture-in-picture and lifecycle behavior intentionally.
- Keep player lifecycle explicit to avoid fragile playback behavior.
Related reading
- How to pop or dismiss a view programmatically
- How to pop or dismiss a view programmatically
- How to POST raw whole JSON in the body of a Retrofit request?
- How to present iOS UIActionSheet in Swift?
- How to present iOS UIActionSheet in Swift?
- How to present popover properly in iOS 8
- How to present popover properly in iOS 8
- How to present UIAlertController when not in a view controller?
.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.