Sample Code

Creating a Movie Player App with Basic Playback Controls

Play movies using a custom interface that implements simple playback functionality.



This sample shows how to create a simple movie playback app using the AVFoundation framework (not AVKit). The AVFoundation objects that manage a player’s visual output don’t present any playback controls. Use the sample to build your own playback controls that implement the functionality to play, pause, fast forward, rewind, and scrub through movies.

Play a Movie in Fast Forward or Reverse

You set the player’s rate property to change the rate of playback. A value of 1.0 plays the movie at its natural rate. Setting the rate to 0.0 is the same as pausing playback using the pause() method. Set the rate property to a number greater than 1.0 to fast forward, and to a number less than 0.0 to reverse.

In this sample, the playFastForward method adds as much as 2.0 to the current player rate property, up to a maximum rate value of 2.0, when the user taps the Fast Forward button in the UI.

// Play fast forward no faster than 2.0.
player.rate = min(player.rate + 2.0, 2.0)

The playReverse method subtracts as much as 2.0 from the current player rate property, down to a minimum rate value of –2.0, when the user taps the Play Reverse button in the UI.

// Reverse no faster than -2.0.
player.rate = max(player.rate - 2.0, -2.0)

You determine whether the player item supports fast forward playback by using the canPlayFastForward property. Similarly, you determine the type of reverse play supported by using:

The setupPlayerObservers method in this sample defines key-value observers on the player and player item properties. These observers enable or disable the fast forward and reverse buttons in the UI based on the reported values of the observed properties.

Play a Movie at its Natural Rate

The togglePlay method toggles the playback state of the movie when the user taps the Play button in the UI. It first uses the timeControlStatus property to determine whether playback is in progress or paused. If the player is paused, the togglePlay method initiates playback of the movie at its natural rate (1.0) by invoking the play() method. If the player is already playing, the togglePlay method pauses playback by invoking the pause() method.

Perform Movie Scrubbing

To handle movie scrubbing, this sample defines the timeSliderDidChange action method on a slider control in the UI. Adjusting the slider control calls this method, which then sets the player time to the new value using the AVPlayer seek(to:) method. The video corresponding to the new time is then rendered in the view.

@IBAction func timeSliderDidChange(_ sender: UISlider) {
    let newTime = CMTime(seconds: Double(sender.value), preferredTimescale: 600) newTime, toleranceBefore: .zero, toleranceAfter: .zero)

This sample also adds a periodic time observer to the player. The observer requests the periodic invocation of a given block during playback to report the current time of the player. The observer invokes the block periodically at the interval specified, interpreted according to the timeline of the current player item. The observer also invokes the block whenever time jumps — for example, during movie scrubbing — and whenever playback starts or stops.

The code in the observer updates the movie’s current time value, displayed by the time slider control in the UI, and keeps it in sync with the movie playback.

timeObserverToken = player.addPeriodicTimeObserver(forInterval: interval,
                                                   queue: .main) { [unowned self] time in
    let timeElapsed = Float(time.seconds)
    self.timeSlider.value = timeElapsed
    self.startTimeLabel.text = self.createTimeString(time: timeElapsed)

See Also

Media Playback

Responding to Playback State Changes

Respond to changes in a player's playback state.

Observing the Playback Time

Observe the playback time for an asset in order to update the player's state.

Seeking Through Media

Seek or scrub across a media item to quickly access a specific time point.

Asset Playback

Play audio and video assets modeled by a player item.

Sample Buffer Playback

Play audio and video sample buffers using a custom player.

QuickTime and ISO-Related Media

Create, play, and modify QuickTime and ISO-related media assets.

Using HEVC Video with Alpha

Play, write, and export HEVC video with an alpha channel to add overlay effects to your video processing.