制作像回旋镖一样的视频循环效果
Posted
技术标签:
【中文标题】制作像回旋镖一样的视频循环效果【英文标题】:Make a video loop effect like boomerang 【发布时间】:2018-01-03 16:55:41 【问题描述】:我正在尝试从 mp4 文件制作视频循环。就像 instagram 应用程序的回旋镖一样,我有这个代码
class ViewController: UIViewController
var avPlayer: AVPlayer!
var avPlayerLayer: AVPlayerLayer!
var paused: Bool = false
var reverse: Bool = true
override func viewDidLoad()
super.viewDidLoad()
let theURL = Bundle.main.url(forResource:"vidio", withExtension: "mp4")
avPlayer = AVPlayer(url: theURL!)
avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
avPlayer.volume = 0
avPlayer.actionAtItemEnd = .none
avPlayerLayer.frame = view.layer.bounds
view.backgroundColor = .clear
view.layer.insertSublayer(avPlayerLayer, at: 0)
NotificationCenter.default.addObserver(self,
selector: #selector(playerItemDidReachEnd(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: avPlayer.currentItem)
func playerItemDidReachEnd(notification: Notification)
guard reverse else
avPlayer.seek(to: kCMTimeZero)
avPlayer.rate = 1.0
reverse = true
print("normal")
return
print("reversa")
avPlayer.seek(to: avPlayer.currentItem!.duration,
toleranceBefore: kCMTimeZero,
toleranceAfter: kCMTimeZero)
avPlayer.rate = -1.0
reverse = false
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear(animated)
avPlayer.play()
paused = false
override func viewDidDisappear(_ animated: Bool)
super.viewDidDisappear(animated)
avPlayer.pause()
paused = true
但是当视频反转时,它看起来并不流畅,并且反转不是从视频的最后一部分开始的。任何想法如何解决这个问题? 我希望视频看起来像在回旋镖应用循环中一样流畅
【问题讨论】:
【参考方案1】:我建议使用图像而不是视频,因为您可以创建一组图像,然后循环遍历它们以获得相同的效果,但不那么费力。
【讨论】:
我们将如何处理图像? 同样的事情只是获取一组图像并遍历它们并添加动画,因此您不会在一瞬间完成所有操作。以上是关于制作像回旋镖一样的视频循环效果的主要内容,如果未能解决你的问题,请参考以下文章