Created
March 10, 2020 19:06
-
-
Save ChrisMash/29da1048ead47913ef4b30d248e7ddd2 to your computer and use it in GitHub Desktop.
An observer for AVPlayer's currentTime that publishes it with Combine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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