Search code examples
iosswiftavplayer

How to play video in reverse in Swift?


I have a recorded in my app video that I want to play in reverse. I use local url for playing in AVPlayer. I found other questions where authors say they check

let canPlayReverse = playViewController.player!.currentItem!.canPlayReverse

And it returns them true and they just set

player.rate = -1.0

But in my case it returns false and it doesn't work


Solution

  • You have seeked to the time kCMTimeZero, which is the beginning of the video. If you play, it doesn't play anything as there is nothing to play before that point. So, seek to the end of the clip:

    .seek(to: playerViewController.player!.currentItem!.asset.duration)
    

    and it should work correctly.