Play video in reverse using AVPlayer and Swift on IOS 9

别说谁变了你拦得住时间么 提交于 2019-12-13 07:09:56

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/33948500/play-video-in-reverse-using-avplayer-and-swift-on-ios-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!