我有一个视频使用avPlayerLayer在图像视图中播放。调用videoViewTapped时,它也会暂停/播放。唯一的事情是,当我希望它全屏显示时,它将不会在用户打开该点时在AVPlayer中恢复该视频。我有timeStopped = player.currentTime()来注册用户选择全屏选项的时间。我查看了Apple文档并尝试使用Seek方法,但是它们都缩短了播放结束时间。
也许我错过了一些东西。我的代码如下。目前,当我按下VC上的全屏按钮时,视频会在几秒钟后冻结。
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var videoView: UIImageView!
@IBOutlet var imageView: UIImageView!
let videoPlayer = AVPlayerViewController()
var player : AVPlayer!
var avPlayerLayer : AVPlayerLayer!
var timeStopped = CMTime()
override func viewDidLoad() {
super.viewDidLoad()
guard let path = Bundle.main.path(forResource: "SampleVideo", ofType:"mp4") else {
debugPrint("Logo-Animation4.mp4 not found")
return
}
player = AVPlayer(url: URL(fileURLWithPath: path))
avPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.videoGravity = AVLayerVideoGravity.resize
videoView.layer.addSublayer(avPlayerLayer)
}
//MARK: VIDEO STUFF
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
avPlayerLayer.frame = videoView.layer.bounds
}
@IBAction func videoViewTapped(_ sender: UITapGestureRecognizer) {
if videoView.tag == 0 {
player.play()
videoView.tag = 1
} else {
player.pause()
timeStopped = player.currentTime()
videoView.tag = 0
}
}
@IBAction func fullScreen(_ sender: Any) {
videoPlayer.player = player
present(videoPlayer, animated: true) {
self.videoPlayer.showsPlaybackControls = true
}
}
}
答案 0 :(得分:0)
您还可以使用videoPlayer中的AVPlayer,这样,当用户进入全屏模式时,您不必重新分配播放器。
let videoPlayer = AVPlayerViewController()
override func viewDidLoad() {
super.viewDidLoad()
videoPlayer.player = AVPlayer(url: URL(fileURLWithPath: path))
}
@IBAction func videoViewTapped(_ sender: UITapGestureRecognizer) {
if videoView.tag == 0 {
videoPlayer.player.play()
videoView.tag = 1
} else {
videoPlayer.player.pause()
videoView.tag = 0
}
}
@IBAction func fullScreen(_ sender: Any) {
present(videoPlayer, animated: true) {
self.videoPlayer.showsPlaybackControls = true
videoPlayer.player.play()
}
}