在IOS 9上使用AVPlayer和Swift反向播放视频 [英] Play video in reverse using AVPlayer and Swift on IOS 9

查看:227
本文介绍了在IOS 9上使用AVPlayer和Swift反向播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要反向播放的录制的视频剪辑.

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

向前播放是可以的,但是只要我搜索到视频文件的末尾并将播放速率设置为-1.0,视频剪辑就会搜索到剪辑的末尾(我在视频上方的时间轴栏中看到了这一点),但不会反向播放.

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)")

这将返回TRUE

然后我寻找剪辑的结尾,然后将播放速率设置为-1.0以进行反向播放.

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")
        }

它向我展示了剪辑已经可以播放了,因此我假设到剪辑结尾(只有2秒长)的搜索已经完成.

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.

然后我播放剪辑:

            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.

我在这里想念什么?

推荐答案

在启动视频后,我决定添加一些日志记录,发现该速率在启动之前为-1,在启动之后立即为1,因此看来速率已重置为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.

这可能是错误吗?

无论如何,在请求播放视频后立即设置速率具有预期效果.

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

这篇关于在IOS 9上使用AVPlayer和Swift反向播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆