Swift
local video playback
iOS development
Swift programming
media player

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.

Browse interview questions

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.

swift
1import UIKit
2import AVKit
3
4final class VideoViewController: UIViewController {
5    private var player: AVPlayer?
6
7    @IBAction func playTapped(_ sender: UIButton) {
8        guard let url = Bundle.main.url(forResource: "intro", withExtension: "mp4") else {
9            print("Bundled video not found")
10            return
11        }
12
13        let player = AVPlayer(url: url)
14        let controller = AVPlayerViewController()
15        controller.player = player
16        self.player = player
17
18        present(controller, animated: true) {
19            player.play()
20        }
21    }
22}

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.

swift
1import UIKit
2import AVKit
3
4func documentURL(fileName: String) -> URL? {
5    FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?
6        .appendingPathComponent(fileName)
7}
8
9final class DownloadedVideoController: UIViewController {
10    private var player: AVPlayer?
11
12    func playDownloadedVideo(named fileName: String) {
13        guard let url = documentURL(fileName: fileName),
14              FileManager.default.fileExists(atPath: url.path) else {
15            print("Sandbox video not found")
16            return
17        }
18
19        let player = AVPlayer(url: url)
20        let controller = AVPlayerViewController()
21        controller.player = player
22        self.player = player
23
24        present(controller, animated: true) {
25            player.play()
26        }
27    }
28}

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.

swift
1import UIKit
2import AVKit
3
4final class EmbeddedVideoController: UIViewController {
5    private let playerController = AVPlayerViewController()
6
7    override func viewDidLoad() {
8        super.viewDidLoad()
9
10        addChild(playerController)
11        playerController.view.translatesAutoresizingMaskIntoConstraints = false
12        view.addSubview(playerController.view)
13        playerController.didMove(toParent: self)
14
15        NSLayoutConstraint.activate([
16            playerController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
17            playerController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
18            playerController.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
19            playerController.view.heightAnchor.constraint(equalToConstant: 240)
20        ])
21    }
22
23    func loadVideo(url: URL) {
24        playerController.player = AVPlayer(url: url)
25    }
26}

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.

swift
1import AVFoundation
2
3func configureAudioSession() {
4    do {
5        let session = AVAudioSession.sharedInstance()
6        try session.setCategory(.playback)
7        try session.setActive(true)
8    } catch {
9        print("Audio session setup failed: \(error)")
10    }
11}

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.url for videos packaged with the app.
  • Use FileManager paths for videos created or downloaded at runtime.
  • 'AVPlayerViewController is 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.