I am trying to add reverse playback to video which I am playing with AVPlayer
:
let videoURL = Bundle.main.url(forResource: "video", withExtension: "mov")
videoPlayer = AVPlayer(url:videoURL!)
let playerLayer = AVPlayerLayer(player: videoPlayer)
playerLayer.frame = self.view.frame
videoView.layer.addSublayer(playerLayer)
videoPlayer.play()
I searched and found if I change AVPlayer
's rate to -1
the movie plays in reverse mode :
func reverseVideo() {
videoPlayer.play()
videoPlayer.rate = -1
}
This does work fine but reverse playback contains lots of lag and it doesn't play smoothly, is there any possible way to fix this issue. I have read other topic in here but did't help.
this code is worked for me to play video in avplayer in reverse from local filesystem.
let duration = (self.playeritem?.asset.duration)!
let durationSec = CMTimeGetSeconds((self.playeritem?.asset.duration)!)
self.avPlayer?.seek(to: duration, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
self.avPlayer?.rate = -1
Try Replacing
videoPlayer.play()
videoPlayer.rate = -1
with
videoPlayer.playImmediately(atRate: -1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With