Listening to Events

The Bitmovin Player provides an extensive Event Framework. Listening to an event from the Player can be implemented in two different ways. Either using the Swift-only Combine API, or by using the listener approach. Both ways cover listening to the events of all other top-level entities, such as PlayerView and Source.

Combine API

Use the Swift-only Combine API to listen to Player.events:

player.events
    .on(TimeChangedEvent.self)
    .sink { timeChangedEvent in
        // Notified for every time changed event
        print("[Event] onTimeChanged (\(event.currentTime))")
    }
    .store(in: &cancellables)

Additional usage

In your SwiftUI views:

VideoPlayerView(player: player)
    .onReceive(player.events.on(TimeChangedEvent.self)) { event in
        // Notified for every time changed event
        print("[Event] onTimeChanged (\(event.currentTime))")
    }

Listener Approach

Use the PlayerListener to listen to a PlayerEvent:

// The type (e.g. PlaybackViewModel) that conforms to the listener must be a subclass of `NSObject`.
extension PlaybackViewModel: PlayerListener {
    func onTimeChanged(_ event: TimeChangedEvent, player: Player) {
        // Notified for every time changed event
        print("[Event] onTimeChanged (\(event.currentTime))")
    }
}