Protocol

AVAsynchronousKeyValueLoading

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

Declaration

@protocol AVAsynchronousKeyValueLoading

Overview

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 playable 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
    default:
        // 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 loadValuesAsynchronouslyForKeys: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.

Topics

Loading Assets by Key

- statusOfValueForKey:error:

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

Required.

- loadValuesAsynchronouslyForKeys:completionHandler:

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

Required.

Constants

AVKeyValueStatus

Constants that specify the load status of a property.

Relationships

Conforming Types

See Also

Track Information

AVAssetTrackGroup

A group of related tracks in an asset.

AVAssetTrackSegment

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

AVFragmentedAssetTrack

An object used for handling tracks of fragmented assets.