Methods you can implement to use an asset or asset track without blocking the calling thread.


protocol AVAsynchronousKeyValueLoading


This protocol includes methods you can use to find out the status of a key—any property of a class that uses asynchronous key value loading. For example, you can find out whether the value of a key has been loaded. You can also ask the object to load its values asynchronously and inform you when the operation has completed.

Because of the nature of timed audiovisual media, successful initialization of an asset doesn't necessarily mean that all of its data is immediately available. Instead, an asset waits to load its data until an operation is performed on it (for example, directly invoking any relevant AVAsset methods, playback via an AVPlayerItem object, export using AVAssetExportSession, reading using an instance of AVAssetReader, and so on). Although you can request the value of any key at any time, and its value will be returned synchronously, the calling thread may be blocked until the request can be satisfied. To avoid blocking:

The following example shows how to asynchronously load an asset’s isPlayable property:

// URL of a bundle asset called 'example.mp4'
let url = Bundle.main.url(forResource: "example", withExtension: "mp4")!
let asset = AVAsset(url: url)
let playableKey = "playable"

// Load the "playable" property
asset.loadValuesAsynchronously(forKeys: [playableKey]) {
    var error: NSError? = nil
    let status = asset.statusOfValue(forKey: playableKey, error: &error)
    switch status {
    case .loaded:
        // Sucessfully loaded. Continue processing.
    case .failed:
        // Handle error
    case .cancelled:
        // Terminate processing
        // Handle all other cases

Even for use cases that may typically support ready access to some keys (such as for assets initialized with URLs for files in the local file system), slow I/O may require AVAsset to block before returning the key's values. Although blocking may be acceptable for macOS clients if you're preparing assets on background threads or in operation queues, when blocking should be avoided, use the loadValuesAsynchronously(forKeys:completionHandler:) method. iOS and tvOS applications should always load values asynchronously using that method because blocking I/O requests may result in a termination of media services.


Loading Assets by Key

func statusOfValue(forKey: String, error: NSErrorPointer) -> AVKeyValueStatus

Returns a status indicating whether the value for a given key is immediately available without blocking the calling thread.


func loadValuesAsynchronously(forKeys: [String], completionHandler: (() -> Void)?)

Tells the asset to load the values of all of the specified keys (property names) that are not already loaded.



enum AVKeyValueStatus

Constants that specify the load status of a property.


Conforming Types

See Also

Track Information

class AVAssetTrackGroup

A group of related tracks in an asset.

class AVAssetTrackSegment

A segment of an asset track, consisting of a time mapping from the source to the asset track timeline.

class AVFragmentedAssetTrack

An object used for handling tracks of fragmented assets.