AVPlayer is a controller object used to manage the playback and timing of a media asset. It provides the interface to control the player’s transport behavior such as its ability to play, pause, change the playback rate, and seek to various points in time within the media’s timeline. You can use an
AVPlayer to play local and remote file-based media, such as QuickTime movies and MP3 audio files, as well as audiovisual media served using HTTP Live Streaming.
- iOS 4.0+
- macOS 10.7+
- tvOS 4.0+
You use an
AVPlayer to play media assets, which AVFoundation models using the
AVAsset only models the static aspects of the media, such as its duration or creation date, and on its own, is unsuitable for playback with an
AVPlayer. To play an asset, you need to create an instance of its dynamic counterpart found in
AVPlayerItem. This object models the timing and presentation state of an asset played by an instance of
AVPlayer. See the
AVPlayerItem reference for more details.
AVPlayer is a dynamic object whose state continuously changes. There are two approaches you can use to observe a player’s state:
General State Observations: You can use Key-value observing (KVO) to observe state changes to many of the player’s dynamic properties, such as its
currentItemor its playback
rate. You should register and unregister for KVO change notifications on the main thread. This avoids the possibility of receiving a partial notification if a change is being made on another thread. AV Foundation invokes
observeValue(forKeyPath:of:change:context:)on the main thread, even if the change operation is made on another thread.
Timed State Observations: KVO works well for general state observations, but isn’t intended for observing continuously changing state like the player’s time.
AVPlayerprovides two methods to observe time changes:
These methods let you observe time changes either periodically or by boundary, respectively. As changes occur, the callback block or closure you supply to these methods is invoked giving you the opportunity to take some action such as updating the state of your player’s user interface.
AVPlayerItem are nonvisual objects meaning that on their own are unable to present an asset’s video on screen. You have two primary approaches you can use to present your video content on screen:
AVKit: The best way to present your video content is by using the AVKit framework’s
AVPlayerViewControllerclass in iOS and tvOS or the
AVPlayerViewclass in macOS. These classes present the video content, along with playback controls and other media features giving you a full-featured playback experience.
AVPlayerLayer: If you are building a custom interface for your player, you use a Core Animation
CALayersubclass provided by AVFoundation called
AVPlayerLayer. The player layer can be set as a view’s backing layer or can be added directly to the layer hierarchy. Unlike
AVPlayerViewController, a player layer doesn’t present any playback controls, but simply presents the visual content on screen. It is up to you to build the playback transport controls to play, pause, and seek through the media.
Alongside the visual content presented with AVKit or
AVPlayerLayer, you can also present animated content synchronized with the player’s timing using
AVSynchronizedLayer. This is a special Core Animation
CALayer subclass, that is used to confer the current player timing onto its layer subtree. You can use
AVSynchronizedLayer to build custom effects in Core Animation, such as animated lower thirds or video transitions, and have them play in sync with the timing of the player’s current
External Playback Mode
External playback mode is when video data is sent to an external device such as Apple TV via AirPlay and the mini-connector-based HDMI/VGA adapters for full screen playback at its original fidelity. AirPlay Video playback is also considered as an "external playback" mode.
In "external screen" mode (also known as mirroring and second display), video data is rendered on the host device (e.g. iPhone), rendered video is recompressed and transferred to the external device, and the external device decompresses and displays the video.
External playback properties affect AirPlay Video playback and are the replacement for the deprecated AirPlay support properties.