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:
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
You have to set the AVPlayer back to beginning of the stream using prepareToPlay and setting the currentPlaybackTime to 0.
videoPlayer.currentPlaybackTime = 0
videoPlayer.prepareToPlay()
Best way to do this is to use AVPlayerViewController
and 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()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.