Created
February 24, 2026 13:05
-
-
Save esnssr/8ede0383b07d237fb31313c88e8850e0 to your computer and use it in GitHub Desktop.
`AsyncStream` periodicTimeValues for AVPlayer
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 Foundation | |
| import AVFoundation | |
| public extension AVPlayer { | |
| func periodicTimeValues( | |
| forInterval interval: CMTime, | |
| queue: dispatch_queue_t? = nil | |
| ) -> AsyncStream<TimeInterval> { | |
| let (stream, continuation) = AsyncStream.makeStream(of: Double.self) | |
| nonisolated(unsafe) var timeObserver: Any? = self.addPeriodicTimeObserver(forInterval: interval, queue: queue) { time in | |
| let seconds = time.seconds | |
| if !seconds.isNaN && seconds.isFinite { | |
| continuation.yield(seconds) | |
| } | |
| } | |
| continuation.onTermination = { _ in | |
| if let observer = timeObserver { | |
| self.removeTimeObserver(observer) | |
| timeObserver = nil | |
| } | |
| } | |
| return stream | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment