videoPlayer = (AVPlayer.init(url: url as URL) as AVPlayer?)!
videoPlayer.actionAtItemEnd = .none
var videoLayer = AVPlayerLayer(player: videoPlayer)
videoLayer.frame = CGRect(x: 0, y: 0, width: (pickVideo?.frame.width)!, height: (pickVideo?.frame.height)!)
pickVideo?.layer.addSublayer(videoLayer)
videoLayer.addSublayer((closeImage?.layer)!)
let tapGueturePlayVideo = UITapGestureRecognizer(target: self, action: #selector(playVideo(sender:)))
tapGueturePlayVideo.delegate = self
tapGueturePlayVideo.numberOfTapsRequired = 1
pickVideo?.addGestureRecognizer(tapGueturePlayVideo)
pickVideo?.isUserInteractionEnabled = true
我在这里创建了Tapgesture的功能:
func playVideo(sender: UITapGestureRecognizer) {
self.videoPlayer.play()
}
如果长按并点按视频,它会播放流畅。如果我在屏幕旁边轻拍并尝试再次播放它不播放.. 点击时断点,但玩家不玩
答案 0 :(得分:0)
您必须使用prepareToPlay将AVPlayer设置回流的开头,并将currentPlaybackTime设置为0.
videoPlayer.currentPlaybackTime = 0
videoPlayer.prepareToPlay()
答案 1 :(得分:0)
最好的方法是使用AVPlayerViewController
和contentOverlayView
就像那样:
override func viewDidLoad() {
super.viewDidLoad()
let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!
videoPlayer = AVPlayer(url: videoURL)
let avPlayerController = AVPlayerViewController()
avPlayerController.player = videoPlayer
avPlayerController.view.frame = CGRect(x: 100, y: 50, width:200, height: 300)
avPlayerController.showsPlaybackControls = false
self.view.addSubview(avPlayerController.view)
let tapGesture:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.playVideo(sender:)))
tapGesture.numberOfTapsRequired = 1
avPlayerController.contentOverlayView?.addGestureRecognizer(tapGesture)
}
@objc func playVideo(sender: UITapGestureRecognizer) {
self.videoPlayer?.play()
}