An object that provides the interface to control the player’s transport behavior.
SDKs
- iOS 4.0+
- macOS 10.7+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- AVFoundation
Declaration
class AVPlayer : NSObject
Overview
An 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 AVPlayer
, called 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
class. 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
current
or its playbackItem 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 invokesobserve
on the main thread, even when making the change operation on another thread.Value(for Key Path: of: 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.
AVPlayer
provides 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
and 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
AVPlayer
class in iOS and tvOS, or theView Controller AVPlayer
class in macOS. These classes present the video content, along with playback controls and other media features giving you a full-featured playback experience.View 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. UnlikeLayer AVPlayer
andView 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.View Controller
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 AVPlayer
.
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.