iOS Developer Library

Developer

AVFoundation Framework Reference AVAssetReader Class Reference

Options
Deployment Target:

On This Page
Language:

AVAssetReader

Inherits From


Conforms To


Import Statement


Swift

import AVFoundation

Objective-C

@import AVFoundation;

Availability


Available in iOS 4.1 and later

You use an AVAssetReader object to obtain media data of an asset, whether the asset is file-based or represents an assemblage of media data from multiple sources (as with an AVComposition object).

AVAssetReader lets you:

AVAssetReader’s pipelines are multithreaded internally. After you initiate reading with initWithAsset:error:, a reader loads and processes a reasonable amount of sample data ahead of use so that retrieval operations such as copyNextSampleBuffer (AVAssetReaderOutput) can have very low latency.

  • Initializes an asset reader for reading media data from a specified asset.

    Declaration

    Swift

    init!(asset asset: AVAsset!, error outError: NSErrorPointer)

    Objective-C

    - (instancetype)initWithAsset:(AVAsset *)asset error:(NSError **)outError

    Parameters

    asset

    The asset from which media data is to be read.

    outError

    If initialization of the reader fails, upon return contains an error that describes the problem.

    Return Value

    An asset reader, initialized for reading media data from asset.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 4.1 and later

  • Returns an asset reader for reading media data from a specified asset.

    Declaration

    Objective-C

    + (instancetype)assetReaderWithAsset:(AVAsset *)asset error:(NSError **)outError

    Parameters

    asset

    The asset from which media data is to be read.

    outError

    If initialization of the reader fails, upon return contains an error that describes the problem.

    Return Value

    An asset reader, initialized for reading media data from asset.

    Import Statement

    Objective-C

    @import AVFoundation;

    Availability

    Available in iOS 4.1 and later

  • outputs outputs Property

    The outputs from which clients of reader can read media data. (read-only)

    Declaration

    Swift

    var outputs: [AnyObject]! { get }

    Objective-C

    @property(nonatomic, readonly) NSArray *outputs

    Discussion

    The array contains concrete instances of AVAssetReaderOutput associated with the reader.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 4.1 and later

  • Adds a given output to the receiver.

    Declaration

    Swift

    func addOutput(_ output: AVAssetReaderOutput!)

    Objective-C

    - (void)addOutput:(AVAssetReaderOutput *)output

    Parameters

    output

    The reader output to add.

    Discussion

    Outputs are created with a reference to one or more AVAssetTrack objects. Adding an output to an asset reader indicates to the reader that it should source from those tracks. The tracks must be owned by the asset returned by the reader’s asset property.

    You cannot add an output after reading has started.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 4.1 and later

  • Returns a Boolean value that indicates whether a given output can be added to the receiver.

    Declaration

    Swift

    func canAddOutput(_ output: AVAssetReaderOutput!) -> Bool

    Objective-C

    - (BOOL)canAddOutput:(AVAssetReaderOutput *)output

    Parameters

    output

    The reader output to be tested.

    Return Value

    YEStrue if output can be added to the receiver, otherwise NOfalse.

    Discussion

    You cannot add an output that reads from a track of an asset other than the asset used to initialize the receiver.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 4.1 and later

  • status status Property

    The status of the reading of sample buffers from the asset. (read-only)

    Declaration

    Swift

    var status: AVAssetReaderStatus { get }

    Objective-C

    @property(readonly) AVAssetReaderStatus status

    Discussion

    This property is thread safe. For possible values, see AVAssetReaderStatus.

    The value of this property indicates whether reading is in progress, has completed successfully, has been canceled, or has failed. You should check the value of this property after the AVAssetReaderOutput method copyNextSampleBuffer returns NULL to determine why no more samples could be read.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 4.1 and later

  • Prepares the receiver for obtaining sample buffers from the asset.

    Declaration

    Swift

    func startReading() -> Bool

    Objective-C

    - (BOOL)startReading

    Return Value

    YEStrue if the reader is able to start reading, otherwise NOfalse.

    Discussion

    This method validates the entire collection of settings for outputs for tracks, for audio mixdown, and for video composition and initiates reading of all outputs.

    status signals the terminal state of the asset reader, and if a failure occurs, error describes the failure.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 4.1 and later

  • Cancels any background work and prevents the receiver’s outputs from reading more samples.

    Declaration

    Swift

    func cancelReading()

    Objective-C

    - (void)cancelReading

    Discussion

    If you want to stop reading samples from the receiver before reaching the end of its time range, you should call this method to stop any background read ahead operations that the may have been in progress.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 4.1 and later

  • error error Property

    Describes the error that occurred if the status is AVAssetReaderStatusFailed. (read-only)

    Declaration

    Swift

    var error: NSError! { get }

    Objective-C

    @property(readonly) NSError *error

    Discussion

    This property is thread safe.

    The value of this property describes what caused the reader to no longer be able to read its asset. If the reader’s status is not AVAssetReaderStatusFailed, the value of this property is nil.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 4.1 and later

  • timeRange timeRange Property

    The time range of the asset that should be read.

    Declaration

    Swift

    var timeRange: CMTimeRange

    Objective-C

    @property(nonatomic) CMTimeRange timeRange

    Discussion

    The intersection of the value of this property and CMTimeRangeMake(kCMTimeZero, asset.duration) determines the time range of the asset from which media data will be read.

    The default value is CMTimeRangeMake(kCMTimeZero, kCMTimePositiveInfinity).

    You cannot change the value of this property after reading has started.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 4.1 and later

  • asset asset Property

    The asset with which the receiver was initialized. (read-only)

    Declaration

    Swift

    var asset: AVAsset! { get }

    Objective-C

    @property(nonatomic, retain, readonly) AVAsset *asset

    Discussion

    Concrete instances of AVAssetReader with specific AVAssetTrack instances must obtain those tracks from the asset returned by this property.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 4.1 and later

  • Constants that indicate the reader’s status.

    Declaration

    Swift

    enum AVAssetReaderStatus : Int { case Unknown case Reading case Completed case Failed case Cancelled }

    Objective-C

    enum { AVAssetReaderStatusUnknown = 0, AVAssetReaderStatusReading, AVAssetReaderStatusCompleted, AVAssetReaderStatusFailed, AVAssetReaderStatusCancelled, }; typedef NSInteger AVAssetReaderStatus;

    Constants

    • Unknown

      AVAssetReaderStatusUnknown

      Indicates that startReading has not yet been invoked.

      Available in iOS 4.1 and later

    • Reading

      AVAssetReaderStatusReading

      Indicates that the reader is ready to provide more sample buffers to its outputs.

      Available in iOS 4.1 and later

    • Completed

      AVAssetReaderStatusCompleted

      Indicates that the reader has provided all available sample buffers to all of its outputs.

      Available in iOS 4.1 and later

    • Failed

      AVAssetReaderStatusFailed

      Indicates that reading failed.

      Available in iOS 4.1 and later

    • Cancelled

      AVAssetReaderStatusCancelled

      Indicates that reading was cancelled using cancelReading.

      Available in iOS 4.1 and later

    Discussion

    You access the reader’s status using the status property.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 4.1 and later