Contents

addPeriodicTimeObserver(forInterval:queue:using:)

Requests the periodic invocation of a given block during playback to report changing time.

Declaration

nonisolated func addPeriodicTimeObserver(forInterval interval: CMTime, queue: dispatch_queue_t?, using block: @escaping  @Sendable (CMTime) -> Void) -> Any

Parameters

  • interval:

    The time interval at which the system invokes the block during normal playback, according to progress of the player’s current time.

  • queue:

    The dispatch queue on which the system calls the block. Passing a concurrent queue isn’t supported and results in undefined behavior.

    If you pass NULL, the system uses the main queue.

  • block:

    The block that the system periodically invokes.

    The block takes a single parameter:

    time

    The time at which the system invokes the block.

Mentioned in

Return Value

An opaque object that you pass as the argument to removeTimeObserver(_:) to cancel observation.

Discussion

You must maintain a strong reference to the returned value as long as you want the time observer to be invoked by the player. You must pair each invocation of this method with a corresponding call to removeTimeObserver(_:). Releasing the observer object without invoking removeTimeObserver(_:) results in undefined behavior.

The system invokes the block periodically at the interval specified, interpreted according to the timeline of the current item. It also invokes the block whenever time jumps or playback starts or stops. If the interval corresponds to a very short interval in real time, the player may invoke the block less frequently than your app requested. Even so, the player will invoke the block sufficiently often for the client to update indications of the current time appropriately in its end-user interface.

The following example illustrates how you set up a callback the system invokes every half second during normal playback.

See Also

Observing playback time