This is my function with which I am trying to play video on UI View,
func playVideo(videoString: String) {
if let videoURL = URL(string: videoString) {
self.player = AVPlayer(url: videoURL)
self.player?.isMuted = true
self.player?.actionAtItemEnd = .none
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
playerLayer.bounds = CGRect(origin: .zero, size: self.videoView.bounds.size)
playerLayer.position = CGPoint(x: self.videoView.bounds.midX, y: self.videoView.bounds.midY)
self.videoView.layer.addSublayer(playerLayer)
self.player?.play()
NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
}
}
@objc func playerItemDidReachEnd(notification: NSNotification {
self.player?.seek(to: CMTime.zero)
self.player?.play()
}
The AVPlayerLayer is not covering the entire UI View, how can make it cover the entire view?
I am trying to play a video on UIView, but AVPlayerLayer is not covering the entire view.
You need to comment this line
As it makes the top left corner of
playerLayerin the middle of thevideoView