Article

Seeking Through Media

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

Overview

In addition to normal, linear playback, users also want the ability to seek or scrub in a nonlinear manner to quickly get to various points of interest within the media. AVKit automatically provides a scrubbing control for you (if supported by the media), but if you’re building a custom player, you’ll need to build this feature yourself. Even in cases where you’re using AVKit, you still may want to provide a supplemental user interface, such as a table view or a collection view, that lets users quickly skip to various locations in the media.

Jump to a Specific Time Quickly

You can seek in a number of ways using the methods of AVPlayer and AVPlayerItem. The most common way is to use the player’s seek(to:) method, passing it a destination CMTime value as follows:

// Seek to the 2 minute mark
let time = CMTime(value: 120, timescale: 1)
player.seek(to: time)

The seek(to:) method is a convenient way to quickly seek through your presentation, but it’s tuned more for speed rather than precision. This means the actual time to which the player moves may differ from the time you requested.

Jump to a Specific Time Accurately

If you need to implement precise seeking behavior, use the seek(to:toleranceBefore:toleranceAfter:) method, which lets you indicate the tolerated amount of deviation from your target time (before and after). If you need to provide sample-accurate seeking behavior, you can indicate that zero tolerance is allowed:

// Seek to the first frame at 3:25 mark
let seekTime = CMTime(seconds: 205, preferredTimescale: Int32(NSEC_PER_SEC))
player.seek(to: seekTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)

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.

Asset Playback

Play audio or video assets modeled by a player item.

Creating a Movie Player App with Basic Playback Controls

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

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.