Class

AVAsset

AVAsset is an abstract, immutable class used to model timed audiovisual media such as videos and sounds. An asset may contain one or more tracks that are intended to be presented or processed together, each of a uniform media type, including but not limited to audio, video, text, closed captions, and subtitles.

Overview

An AVAsset defines the collective properties of the tracks that comprise the asset. (You can access the instances of AVAssetTrack representing tracks of the collection, so you can examine each of these independently if you need to.)

You often instantiate an asset using AVURLAsset—a concrete subclass of AVAsset—with URLs that refer to audiovisual media resources, such as streams (including HTTP live streams), QuickTime movie files, MP3 files, and files of other types. 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.

Properties of assets as a whole are defined by AVAsset. Additionally, references to instances of AVAssetTrack representing tracks of the collection can be obtained, so that each of these can be examined independently.

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 asset always returns its value synchronously, although it may have to block the calling thread in order to do so. In order to avoid blocking, you can register your interest in particular keys and to become 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, etc.), and provide the player item to an AVPlayer object according to whether the item is to be played by itself or together with a collection of other items.

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

Subclassing Notes

It is not currently possible to subclass AVAsset to handle streaming protocols or file formats that are not supported by the framework.

Symbols

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?

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 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. For example, the value of isPlayable is true even if the content of the asset is protected and requires special authorization for use, even before that authorization is obtained. Similarly, the value of isExportable is true even if only some of the AVAssetExportSession presets can be used. These properties will only return false when there is no condition under which the associated operation can be performed.

var isPlayable: Bool

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

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: String)

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

func tracks(withMediaCharacteristic: String)

Returns an array of AVAssetTrack objects of the asset that present media with a specified characteristic.

func unusedTrackID()

Returns an ID that is currently unused by any of the tracks in the asset.

Accessing Metadata

var lyrics: String?

The lyrics of the asset 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: [String]

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

func metadata(forFormat: String)

Returns an array of AVMetadataItem objects, one for each metadata item in the container of the specified format.

Using Chapter Metadata

var availableChapterLocales: [Locale]

The locales available for chapter metadata in the asset.

func chapterMetadataGroups(withTitleLocale: Locale, containingItemsWithCommonKeys: [String]?)

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 the list of preferred languages.

Finding Media Selections

var preferredMediaSelection: AVMediaSelection

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

var availableMediaCharacteristicsWithMediaSelectionOptions: [String]

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

func mediaSelectionGroup(forMediaCharacteristic: String)

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

Indicates whether the asset is capable of being extended by fragments.

var containsFragments: Bool

Indicates whether the asset is extended by at least one movie fragment.