如何添加点击手势来播放视频播放器

时间:2021-10-29 08:24:03
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

And I have created function for Tapgesture here:

我在这里创建了Tapgesture的功能:

func playVideo(sender: UITapGestureRecognizer) {

    self.videoPlayer.play()

}

If long press and tap the video it plays smooth..If I tap out side the screen and tried to play another time it does not play.. Break point calls while tapping but player not playing

如果长按并点按视频,它会播放流畅。如果我在屏幕旁边轻拍并尝试再次播放则无法播放..点击时断点,但播放器不播放

2 个解决方案

#1


0  

You have to set the AVPlayer back to beginning of the stream using prepareToPlay and setting the currentPlaybackTime to 0.

您必须使用prepareToPlay将AVPlayer设置回流的开头,并将currentPlaybackTime设置为0。

AVPlayer doesn't play more than one time

AVPlayer不会播放多次

videoPlayer.currentPlaybackTime = 0
videoPlayer.prepareToPlay()

#2


0  

Best way to do this is to use AVPlayerViewController and contentOverlayView

最好的方法是使用AVPlayerViewController和contentOverlayView

Like that :

像那样 :

 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()

    }

#1


0  

You have to set the AVPlayer back to beginning of the stream using prepareToPlay and setting the currentPlaybackTime to 0.

您必须使用prepareToPlay将AVPlayer设置回流的开头,并将currentPlaybackTime设置为0。

AVPlayer doesn't play more than one time

AVPlayer不会播放多次

videoPlayer.currentPlaybackTime = 0
videoPlayer.prepareToPlay()

#2


0  

Best way to do this is to use AVPlayerViewController and contentOverlayView

最好的方法是使用AVPlayerViewController和contentOverlayView

Like that :

像那样 :

 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()

    }