The abstract class used to model timed audiovisual media such as videos and sounds.


class AVAsset : NSObject


An AVAsset defines the collective properties of the tracks that comprise the asset. You create an AVAsset by initializing it with a local or remote URL pointing to a media resource, as shown in the following example:

let url: URL = // Local or Remote Asset URL
let asset = AVAsset(url: url)

AVAsset is an abstract class, so when you create an asset as shown in the example, you’re actually creating an instance of one of its concrete subclasses called AVURLAsset. In many cases this is a suitable way of creating an asset, but you can also directly instantiate an AVURLAsset when you need more fine-grained control over its initialization. The initializer for AVURLAsset accepts an options dictionary, with which you tailor the asset’s initialization to your particular use case. For instance, if you’re creating an asset for an HLS stream, you may want to prevent it from retrieving its media when a user is connected to a cellular network. You can do this as shown in the following example:

let url: URL = // Remote Asset URL
let options = [AVURLAssetAllowsCellularAccessKey: false]
let asset = AVURLAsset(url: url, options: options)

You can also instantiate an asset using other concrete subclasses that extend the basic model for audiovisual media in useful ways, as AVComposition does for temporal editing.

Because of the nature of timed audiovisual media, upon successful initialization of an asset, some or all of the values for its keys may not be immediately available. The value of any key can be requested at any time, and the asset always returns its value synchronously, although it may have to block the calling thread to do so. To avoid blocking, you can register your interest in particular keys and be notified when their values become available. For further details, see AVAsynchronousKeyValueLoading.

To play an instance of AVAsset, initialize an instance of AVPlayerItem with it, use the player item to set up its presentation state (such as whether only a limited timeRange of the asset should be played), and provide the player item to an AVPlayer object according to whether the item is to be played by itself or with a collection of other items.

You can insert AVAsset objects into an AVMutableComposition object to assemble audiovisual constructs from one or more source assets.

Subclassing Notes

It isn't currently possible to subclass AVAsset to handle streaming protocols or file formats that aren't supported by the framework.


Creating an Asset

init(url: URL)

Returns an asset for inspection of a media resource.

Loading Data

func cancelLoading()

Cancels the loading of all values for all observers.

Inspecting an Asset

var duration: CMTime

The duration of the asset.

var providesPreciseDurationAndTiming: Bool

A Boolean value that indicates whether the asset provides precise timing.

var creationDate: AVMetadataItem?

A value that indicates the creation date of the asset.

var naturalSize: CGSize

The encoded or authored size of the visual portion of the asset.

var preferredRate: Float

The natural rate at which the asset is to be played.

var preferredTransform: CGAffineTransform

The preferred transform to apply to the visual content of the asset for presentation or processing.

var preferredVolume: Float

The preferred volume at which the audible media of the asset is to be played.

var preferredDisplayCriteria: AVDisplayCriteria

The display criteria that define the optimal TV display mode for the asset.

class AVDisplayCriteria

An object containing properties that define the display criteria for a media asset.

Determining Usability

The usability properties have a value of true when the indicated operation can be performed, even if only true under a specific set of conditions. These properties only return false when there is no condition under which the associated operation can be performed.

var isPlayable: Bool

A Boolean value that indicates whether the asset, or its URL, can be used to initialize an instance of a player item.

var isExportable: Bool

A Boolean value that indicates whether the asset can be exported using an asset export session.

var isReadable: Bool

A Boolean value that indicates whether the asset’s media data can be extracted using an asset reader.

var isComposable: Bool

A Boolean value that indicates whether the asset can be used within a segment of a composition track.

var hasProtectedContent: Bool

A Boolean value that indicates whether the asset has protected content.

var isCompatibleWithAirPlayVideo: Bool

A Boolean value that indicates whether the asset is compatible with AirPlay Video.

var isCompatibleWithSavedPhotosAlbum: Bool

A Boolean value that indicates whether the asset can be written to the Saved Photos album.

Accessing Tracks

var tracks: [AVAssetTrack]

The collection of all tracks contained by the asset.

var trackGroups: [AVAssetTrackGroup]

An array containing all of the track groups in the asset.

func track(withTrackID: CMPersistentTrackID) -> AVAssetTrack?

Returns the track associated with a specified track identifier.

func tracks(withMediaType: AVMediaType) -> [AVAssetTrack]

Returns an array of asset tracks associated with an asset for the specified media type.

func tracks(withMediaCharacteristic: AVMediaCharacteristic) -> [AVAssetTrack]

Returns an array of asset tracks associated with an asset for the specified characteristic.

func unusedTrackID() -> CMPersistentTrackID

Returns an ID that's unused by any of the tracks in the asset.

Accessing Metadata

var lyrics: String?

The lyrics of the asset, in a language suitable for the current locale.

var metadata: [AVMetadataItem]

An array of metadata items for all metadata identifiers for which a value is available.

var commonMetadata: [AVMetadataItem]

An array of metadata items for each common metadata key for which a value is available.

var availableMetadataFormats: [AVMetadataFormat]

An array of strings, each representing a metadata format that’s available to the asset.

func metadata(forFormat: AVMetadataFormat) -> [AVMetadataItem]

Returns an array of metadata items found in the container with the specified format.

Using Chapter Metadata

Presenting Chapter Markers

Add chapter markers to enable users to quickly navigate your content.

var availableChapterLocales: [Locale]

The locales available for chapter metadata in the asset.

func chapterMetadataGroups(withTitleLocale: Locale, containingItemsWithCommonKeys: [AVMetadataKey]?) -> [AVTimedMetadataGroup]

Returns an array of chapters with a given title locale and containing specified keys.

func chapterMetadataGroups(bestMatchingPreferredLanguages: [String]) -> [AVTimedMetadataGroup]

Returns an array of chapters whose locale best matches the list of preferred languages.

Finding Media Selections

var preferredMediaSelection: AVMediaSelection

The default media selections for this asset’s media selection groups.

var availableMediaCharacteristicsWithMediaSelectionOptions: [AVMediaCharacteristic]

An array of media characteristics for which a media selection option is available.

func mediaSelectionGroup(forMediaCharacteristic: AVMediaCharacteristic) -> AVMediaSelectionGroup?

Returns a media selection group object that contains one or more options with the specified media characteristic.

Managing Reference Restrictions

var referenceRestrictions: AVAssetReferenceRestrictions

The asset’s reference restrictions.

Working with Fragmented Movies

var canContainFragments: Bool

A Boolean value that indicates whether the asset is capable of being extended by fragments.

var containsFragments: Bool

A Boolean value that indicates whether the asset is extended by at least one movie fragment.

var overallDurationHint: CMTime

The total duration of fragments that currently exist, or may exist in the future.

Specifying Media Characteristics

struct AVMediaCharacteristic

The options for specifying media type characteristics.

See Also

Media Assets

About the Asset Model

Understand how assets are used as the building blocks for media players.

class AVAssetTrack

An object that provides the track-level inspection interface for an asset’s media tracks.

Asset Manipulation

Retrieve assets for playback or to gather information about an asset.

Asset File Import and Export

Import and export media samples to and from the file system.