Skip to content

Instantly share code, notes, and snippets.

@ChrisMash
Created March 10, 2020 19:06
Show Gist options
  • Select an option

  • Save ChrisMash/29da1048ead47913ef4b30d248e7ddd2 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisMash/29da1048ead47913ef4b30d248e7ddd2 to your computer and use it in GitHub Desktop.
An observer for AVPlayer's currentTime that publishes it with Combine
import Combine
class PlayerTimeObserver {
let publisher = PassthroughSubject<TimeInterval, Never>()
private var timeObservation: Any?
init(player: AVPlayer) {
// Periodically observe the player's current time, whilst playing
timeObservation = player.addPeriodicTimeObserver(forInterval: CMTime(seconds: 0.5, preferredTimescale: 600), queue: nil) { [weak self] time in
guard let self = self else { return }
// Publish the new player time
self.publisher.send(time.seconds)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment