我试图从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
}
}
但是当视频反转时,它看起来并不平滑,反之亦然。从视频的最后部分开始。任何想法如何解决这个问题? 我希望视频看起来像在飞旋镖应用程序循环中一样平滑
答案 0 :(得分:0)
我建议使用Images而不是视频,因为你可以创建一个图像数组,然后循环遍历它们以获得相同的效果,但不那么挣扎。