How to play a local video with Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Playing a local video in an iOS app usually means giving AVPlayer a valid file URL and presenting a player UI. The main implementation details are not the playback API itself, but where the file lives, how long the player is retained, and whether the video should appear full-screen or inline.
Play a Video Bundled With the App
If the video ships with the app, add it to the Xcode project and make sure target membership is correct. Then load the file from the main bundle.
This is the quickest route to reliable playback controls because AVPlayerViewController handles a lot of media UI for you.
Play a Video From the Sandbox
If the video was downloaded, exported, or generated at runtime, it will usually live in the app sandbox rather than the bundle. In that case, build the URL from FileManager.
This is the right pattern whenever the file path is only known at runtime.
Embed the Player Inline
Sometimes a full-screen modal player is not what the screen needs. If the video is part of a larger layout, embed AVPlayerViewController as a child view controller.
This approach is useful for course content, product pages, and media galleries.
Keep Ownership of the Player Explicit
In small demos, it can look like the local AVPlayer variable is enough. In real apps, it is safer to keep the player in a property. That makes the object's lifetime explicit and avoids subtle cases where playback state or reuse becomes harder to manage.
The same principle applies if you later add observation, playback controls, or analytics hooks.
Configure Audio Behavior When Needed
If video playback is a real media feature rather than a short incidental effect, configure the audio session deliberately.
This matters when silent mode, interruptions, or background audio behavior are part of the expected user experience.
Common Pitfalls
A common mistake is forgetting target membership for a bundled video file. Then Bundle.main.url returns nil even though the file appears in the project navigator.
Another issue is looking in the wrong sandbox directory for runtime files. A correct playback API call cannot fix a wrong file path.
Developers also sometimes assume the simulator is enough to validate codec support. Real-device testing still matters for media features.
Summary
- Use
Bundle.main.urlfor videos packaged with the app. - Use
FileManagerpaths for videos created or downloaded at runtime. - '
AVPlayerViewControlleris the easiest path to reliable playback UI.' - Keep the player in a property when ownership needs to stay explicit.
- Test file paths, target membership, and audio behavior early when debugging playback issues.
Related reading
- How to play a sound using Swift?
- How to play a sound using Swift?
- How to play video with AVPlayerViewController AVKit in Swift
- How to play video with AVPlayerViewController AVKit in Swift
- 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?
.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.