Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with reverse AVPlayer reverse playback

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.

like image 325
iOS.Lover Avatar asked Jun 06 '17 13:06

iOS.Lover


2 Answers

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
like image 136
Dhaval Patel Avatar answered Sep 30 '22 21:09

Dhaval Patel


Try Replacing videoPlayer.play() videoPlayer.rate = -1 with videoPlayer.playImmediately(atRate: -1)

like image 31
Rajat Sharma Avatar answered Sep 30 '22 21:09

Rajat Sharma