An object that provides the interface to control the player’s transport behavior.
- iOS 4.0+
- macOS 10.7+
- tvOS 9.0+
AVPlayer is a controller object used to manage the playback and timing of a media asset. 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.
AVPlayer is for playing a single media asset at a time. You can reuse the player instance to play additional media assets using its
replace method, but it manages the playback of only a single media asset at a time. The framework also provides a subclass of
AVQueue, you use to create and manage the queuing of media assets played sequentially.
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
AVPlayer. This object models the timing and presentation state of an asset played by an instance of
AVPlayer. See the
AVPlayer 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
currentor 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 when making a change on another thread. AVFoundation invokes
observeon the main thread, even when making the change operation on another thread.
Value For Key Path: of Object: change: context:
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, invoke the callback block or closure you supply to these methods to give you the opportunity to take some action such as updating the state of your player’s user interface.
AVPlayer are nonvisual objects, meaning that on their own they are unable to present an asset’s video onscreen. You have two primary approaches you can use to present your video content onscreen:
AVKit: The best way to present your video content is with the AVKit framework’s
AVPlayerclass in iOS and tvOS, or the
AVPlayerclass in macOS. These classes present the video content, along with playback controls and other media features giving you a full-featured playback experience.
AVPlayerLayer: When building a custom interface for your player, use
AVPlayer. The player layer can be set as a view’s backing layer or can be added directly to the layer hierarchy. Unlike
AVPlayer, a player layer doesn’t present any playback controls—it presents the visual content onscreen. It's 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
AVPlayer, you can also present animated content synchronized with the player’s timing using
AVSynchronized. Use the synchronized layer to confer the current player timing onto its layer subtree. You can use
AVSynchronized 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, or the mini-connector-based HDMI/VGA adapters for full-screen playback at its original fidelity.
In "external screen" mode (also known as mirroring and second display), the host device renders the video data. The host device recompresses and transfers the rendered video 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.