- iOS 10.0+
- Xcode 11.3+
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
play 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 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.
You determine whether the player item supports fast forward playback by using the
can property. Similarly, you determine the type of reverse play supported by using:
can— Supports a rate value of –1.0
can— Supports rate values from 0.0 to –1.0
Play Slow Reverse
can— Supports rate values less than –1.0
Play Fast Reverse
setup 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
toggle method toggles the playback state of the movie when the user taps the Play button in the UI. It first uses the
time property to determine whether playback is in progress or paused. If the player is paused, the
toggle method initiates playback of the movie at its natural rate (1.0) by invoking the
play() method. If the player is already playing, the
toggle method pauses playback by invoking the
Perform Movie Scrubbing
To handle movie scrubbing, this sample defines the
time 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
seek(to:) method. The video corresponding to the new time is then rendered in the view.
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.