Class

AVAsset

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

Overview

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.

Topics

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 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.

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 AVPlayerItem.

var isExportable: Bool

A Boolean value that indicates whether the asset can be exported using AVAssetExportSession.

var isReadable: Bool

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

var isComposable: Bool

A Boolean value that indicates whether the asset can be used within a segment of an AVCompositionTrack object.

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)

Returns the track with a specified track ID.

func tracks(withMediaType: AVMediaType)

Returns an array of the asset tracks of the asset that presents media of a specified type.

func tracks(withMediaCharacteristic: AVMediaCharacteristic)

Returns an array of asset tracks of the asset that presents media with a specified characteristic.

func unusedTrackID()

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)

Returns an array of AVMetadataItem objects—one for each metadata item in the container of 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]?)

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

func chapterMetadataGroups(bestMatchingPreferredLanguages: [String])

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)

Returns an AVMediaSelectionGroup object that contains one or more options with the specified media characteristic.

Managing Reference Restrictions

var referenceRestrictions: AVAssetReferenceRestrictions

The reference restrictions being used by the receiver.

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

A Boolean value that indicates the total duration of fragments that either exist now or may be appended in the future in order to extend the duration of the asset.

Specifying Media Characteristics

struct AVMediaCharacteristic

Options for specifying the characteristics of media types.

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 to be used for playback or to get information about an asset; for example, an asset's playing time.

Asset File Import and Export

Import existing media samples from the file system; export new, modified, or existing media samples to the file system.