Instance Method

addPeriodicTimeObserverForInterval:queue:usingBlock:

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

Declaration

- (id)addPeriodicTimeObserverForInterval:(CMTime)interval queue:(dispatch_queue_t)queue usingBlock:(void (^)(CMTime time))block;

Parameters

interval

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

queue

A serial dispatch queue onto which block should be enqueued. Passing a concurrent queue is not supported and will result in undefined behavior.

If you pass NULL, the main queue is used.

block

The block to be invoked periodically.

The block takes a single parameter:

time

The time at which the block is invoked.

Return Value

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

Discussion

You must maintain a strong reference the returned value as long as you want the time observer to be invoked by the player. Each invocation of this method should be paired with a corresponding call to removeTimeObserver:. Releasing the observer object without invoking removeTimeObserver: will result in undefined behavior.

The block is invoked periodically at the interval specified, interpreted according to the timeline of the current item. The block is also invoked whenever time jumps and whenever 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 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 would set up a callback to be fired every half second during normal playback.

- (void)addPeriodicTimeObserver {
    // Invoke callback every half second
    CMTime interval = CMTimeMakeWithSeconds(0.5, NSEC_PER_SEC);
    // Queue on which to invoke the callback
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    // Add time observer
    self.timeObserverToken =
        [self.player addPeriodicTimeObserverForInterval:interval
                                                  queue:mainQueue
                                             usingBlock:^(CMTime time) {
            // Use weak reference to self
            // Update player transport UI
        }];
}

See Also

Observing Time

- addBoundaryTimeObserverForTimes:queue:usingBlock:

Requests the invocation of a block when specified times are traversed during normal playback.

- removeTimeObserver:

Cancels a previously registered periodic or boundary time observer.