How to Play Videos Using AVPlayerViewController in Swift

Explore AVPlayerViewController and Learn to Play Videos Seamlessly in Your Swift App

Vikram Kumar
3 min readNov 5, 2023

Playing videos in an iOS app is a common requirement, and Apple provides a convenient framework called AVKit for handling media playback. AVPlayerViewController, part of AVKit, is a pre-built view controller that simplifies the process of adding video playback to your iOS app. In this article, you’ll learn how to use AVPlayerViewController to play videos in your Swift application.

Photo by John Mark Arnold on Unsplash

What is AVPlayerViewController?

AVPlayerViewController is part of the AVKit framework, introduced in iOS 8, and is used to display and control media playback. It is a pre-built view controller that handles most of the common user interactions and playback controls you need when playing audio and video content.

Here are some key features and capabilities of AVPlayerViewController:

  1. Media Playback: AVPlayerViewController seamlessly plays both audio and video content.
  2. User Interface: It provides a user-friendly interface for controlling playback, including play/pause, volume control, and scrubbing through the media.
  3. AirPlay: It supports AirPlay, allowing users to stream media to other devices.
  4. Picture-in-Picture: AVPlayerViewController supports Picture-in-Picture mode on iPad, enabling users to continue watching videos in a small, resizable player while using other apps.
  5. Subtitle and Audio Track Selection: You can choose from available subtitles and audio tracks if the media supports multiple options.
  6. Customization: While AVPlayerViewController offers a default interface, you can customize it to some extent by setting properties and adding additional controls.

Setting Up the Interface

In your project’s main storyboard or view controller XIB, add a button or any user interface element that will trigger video playback. For example, add a “Play Video” button.

Handle video playback

  1. Open the view controller Swift file associated with your storyboard or XIB.
  2. Import AVKit and AVFoundation at the top of your Swift file
  3. In the view controller class, implement an action for the button. This action will be called when the button is tapped, and it will present the AVPlayerViewController.
  4. Build and run your app on a device or simulator.
  5. When you tap the “Play Video” button, AVPlayerViewController will appear, and your video will start playing.
import AVKit
import AVFoundation

class YourViewController: UIViewController {


override func viewDidLoad() {
super.viewDidLoad()
}


@IBAction func playVideoButtonTapped(_ sender: UIButton) {
// Get the video URL from the main bundle (assuming the video is added to the project)
if let videoURL = Bundle.main.url(forResource: "yourVideoFileName", withExtension: "mp4") {
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player

present(playerViewController, animated: true) {
player.play()
}
}
}
}

Replace "yourVideoFileName" with the actual filename of your video.

Make sure you have a video file that you want to play in your iOS app. You should add the video to your Xcode project, and it should be in a format supported by iOS (e.g., MP4).

Playing Videos from a Remote URL

In addition to playing videos from local files, AVPlayerViewController allows you to easily play videos from remote URLs. Here’s how to do it:

  1. First, make sure you have a valid video URL from a remote source. For this example, let’s assume you have a video hosted on a publicly accessible server.
  2. Modify the button action you previously created to play a video from a remote URL:
@IBAction func playVideoButtonTapped(_ sender: UIButton) {
// Replace with the URL of your remote video
let videoURLString = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"

// Ensure the URL is valid
guard let videoURL = URL(string: videoURLString) else {
print("Invalid video URL")
return
}

let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player

present(playerViewController, animated: true) {
player.play()
}
}

Conclusion

In this article, you’ve learned how to use AVPlayerViewController to play videos in your Swift iOS application. You added a video to your project, set up the interface, and wrote Swift code to present the video using AVPlayerViewController. With this knowledge, you can enhance your app by incorporating media playback seamlessly and enhancing the user experience.

Happy Coding!

--

--

Vikram Kumar
Vikram Kumar

Written by Vikram Kumar

I am Vikram, a Senior iOS Developer at Matellio Inc. focused on writing clean and efficient code. Complex problem-solver with an analytical and driven mindset.

No responses yet