iosswiftvideoreverseavplayer

Play video in reverse using AVPlayer and Swift on IOS 9


I have a recorded video clip that I want to play in reverse.

Playing forward is fine but as soon as I seek to the end of the video file and set the rate of playback to -1.0 the video clip seeks to the end of the clip (I see this in the timeline bar above the video) but does not play in reverse.

After I present the player view controller I check if it is ready to use:

            print("readyForDisplay = \(playerViewController.readyForDisplay)")

This tells me that all is ready to prepare to play.

I check if reverse play is possible:

let reversePlay = playerViewController.player!.currentItem?.canPlayReverse
        print("reversePlay = \(reversePlay)")

This returns TRUE

I then seek to the end of the clip and set the play back rate to -1.0 for reverse play.

playerViewController.player!.seekToTime(playerViewController.player!.currentItem!.asset.duration)
        playerViewController.player!.rate = -1.0

I believe having got this far it is ready to play because if I add the following:

let status : AVPlayerItemStatus? = playerViewController.player!.currentItem?.status

        if status == AVPlayerItemStatus.ReadyToPlay {
            print("Ready to play")
        }

It shows me that the clip is ready to play, so I am assuming that seeking to the end of clip (which is only 2 seconds long) has completed.

I then play the clip:

            playerViewController.player!.play()

It plays fine if I don't seek to the end and attempt to change the rate to set it to reverse play.

What am I missing here?


Solution

  • I decided to add some logging after launching the video and found that the rate was -1 before launching and 1 immediately after so it seems that the rate is reset to 1.

    Could this be a bug?

    Anyway setting the rate immediately after the request to play the video has the desire effect.