The abstract class used to model timed audiovisual media such as videos and sounds.
SDKs
- iOS 4.0+
- macOS 10.7+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- AVFoundation
Declaration
class AVAsset : NSObject
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 AVAsynchronous
.
To play an instance of AVAsset
, initialize an instance of AVPlayer
with it, use the player item to set up its presentation state (such as whether only a limited time
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 AVMutable
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.