Article

Creating a Basic Video Player (macOS)

Create a simple video playback app with built-in controls.

Overview

Using AVKit and AVFoundation, you can quickly create a simple video playback app.

Set Up and Configure the User Interface

Begin by creating the user interface for the player.

  1. Open the Main.storyboard file and drag an AVKit player view object onto the storyboard.

  2. Add pinning constraints to the player view to pin it to its superview’s edges and maintain its aspect ratio, as shown in Figure 1.

  3. Select the player view. In the Attributes inspector, change the Controls Style selection to Floating. This style presents controls matching the ones found in the QuickTime Player app.

Xcode constraints window with pinning and aspect ratio options added.

Implement Playback Behavior

Now that you’ve created the user interface, it’s time to add the code required to play a video.

  1. Open the assistant editor from the Main.storyboard file, Control-drag the player view object to the ViewController.swift class, and add a new @IBOutlet called playerView.

  2. Add the AVKit and AVFoundation frameworks to the ViewController.swift class.

  3. In the viewDidLoad method, add the following code:

override func viewDidLoad() {
    super.viewDidLoad()
    guard let url = URL(string: "https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_ts/master.m3u8") else {
        return
    }
    // Create a new AVPlayer and associate it with the player view
    let player = AVPlayer(url: url)
    playerView.player = player
}

Your app is complete, and you can build and run it on your macOS device.

See Also

First Steps

Guidelines for Apple TV 4K Development

Follow these guidelines when developing for Apple TV 4K.

Creating a Basic Video Player (iOS and tvOS)

Create a simple video playback app with built-in controls.