Observe the playback time for an asset in order to update the player's state.
You’ll commonly want to observe an asset's playback time as it progresses so you can update the playback position or otherwise synchronize the state of your user interface. Although key-value observing (KVO) works well for general state observations, it's not the right choice for observing player timing because it’s not well suited for observing continuous state changes. Instead,
AVPlayer provides two different ways for you to observe player time changes: periodic observations and boundary observations.
Observe Periodic Timing
You can observe time ticking by at some regular, periodic interval. If you’re building a custom player, the most common use case for periodic observation is to update the time display in your user interface.
To observe periodic timing, use the player’s
add method. This method takes a CMTime value representing the time interval, a serial dispatch queue, and a callback block to be invoked at the specified time interval. The following example shows how to set up a block to be called every half-second during normal playback:
Observe Boundary Timing
The other way you can observe time is by boundary. You define various points of interest within the media’s timeline, and the framework calls you back as those times are traversed during normal playback. Boundary observations are used less frequently than periodic observations, but can still prove useful in certain situations. For instance, you might use boundary observations if you're presenting a video with no playback controls and want to synchronize elements of the display or present supplemental content as those times are traversed.
To observe boundary times, use the player’s
add method. This method takes an array of
NSValue objects wrapping the
CMTime values that define your boundary times, a serial dispatch queue, and a callback block. The following example shows how to define boundary times for each quarter of playback: