gpt4 book ai didi

ios - 我如何快速制作循环视频

转载 作者:搜寻专家 更新时间:2023-11-01 05:47:05 26 4
gpt4 key购买 nike

我将如何制作一个没有循环播放声音的本地 .mp4 文件,这样它只会占据屏幕的一部分并且没有用户控件。只是一个循环播放的视频,有点像 gif。我正在使用 xcodeswift2

class ViewController: UIViewController {

var playerViewController = AVPlayerViewController()
var playerView = AVPlayer()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

}

override func viewDidAppear(animated: Bool) {
var fileURL = NSURL(fileURLWithPath: "/Users/Mantas/Desktop/123/123/video-1453562323.mp4.mp4")
playerView = AVPlayer(URL: fileURL)

playerViewController.player = playerView

self.presentViewController(playerViewController, animated: true){

self.playerViewController.player?.play()

}
}
}

我做了这个,它播放视频,但是全屏,我不知道如何让它只占据屏幕的一部分以及如何让它循环播放

最佳答案

Swift 3.0 中的替代版本:

添加这些属性:

fileprivate var player: AVPlayer? {
didSet { player?.play() }
}

fileprivate var playerObserver: Any?

将此添加到您的 deinit 中:

deinit {
guard let observer = playerObserver else { return }
NotificationCenter.default.removeObserver(observer)
}

添加这个函数:

func videoPlayerLayer() -> AVPlayerLayer {
let fileURL = URL(fileURLWithPath: mediaPath)
let player = AVPlayer(url: fileURL)
let resetPlayer = {
player.seek(to: kCMTimeZero)
player.play()
}
playerObserver = NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil) { notification in
resetPlayer()
}
self.player = player
return AVPlayerLayer(player: player)
}

然后在任何你喜欢的地方添加到你的层(viewDidLoad、viewDidAppear、viewDidFinishLayingOutSubviews):

let playerLayer = videoPlayerLayer()
playerLayer.frame = view.bounds
view.layer.insertSublayer(playerLayer, at: 0)

关于ios - 我如何快速制作循环视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34990060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com