I need to add a CABasicAnimation
to an AVPlayerLayer
when an AVQueuePlayer
's playerItem
is one second from finishing (playerItem's duration - 1). It's a basic crossfade. I just can't seem to figure out how to tell when I've arrived at the right time. Using the code below, I've added a periodic time observer. Is there any way to know when my playerItem is one second from ending?
timeObserverToken = player?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main) { [unowned self] time in
let fadeOutTime = CMTime(seconds: 1, preferredTimescale: time.timescale)
let duration = self.currentItem()?.duration
let convertedDuration = CMTimeConvertScale(duration!, time.timescale, .default)
if CMTimeCompare(time, CMTimeSubtract(convertedDuration, fadeOutTime)) == 0 {
self.playerLayer.add(self.fadeOut, forKey: "fadeOut")
}
}