Retrieve and filter all of the metadata associated with an asset.
Media container formats store descriptive metadata about their media. As a developer, it’s often challenging to work with metadata, because each container format has its own unique metadata format. You typically need a low-level understanding of the format to read and write a container’s metadata, but AVFoundation simplifies working with metadata through the use of its
In its most basic form, an instance of
AVMetadata is a key-value pair representing a single metadata value such as a movie’s title or an album’s artwork. In the same way that
AVAsset provides a normalized view of your media,
AVMetadata provides a normalized view of its associated metadata.
Load the Asset's Metadata
You determine what metadata formats an asset contains by calling its
available property. This property returns an array of string identifiers for each metadata format it contains. You then use its
metadata(for method to retrieve format-specific metadata values by passing it an appropriate format identifier, as shown below:
Filter the Metadata Collection
After you’ve retrieved a collection of metadata, the next step is to find the specific values of interest within it. You use the various class methods of
AVMetadata to filter metadata collections down to a discrete set of values.
The easiest way to find specific metadata items is to filter by identifier, which groups a key space and key into a single unit. The following example shows how to retrieve the title item from the common key space:
Find Specific Values
After you’ve retrieved a specific metadata item, the next step is to call its value property. The value returned is an object type adopting the
NSCopying protocols. You can manually cast the value to the appropriate type, but it’s safer and easier to use the metadata item’s type coercion properties. You can use its
data properties to easily coerce the value to the appropriate type. For instance, the following example shows how you retrieve the artwork associated with an iTunes music track: