Trying to run video on a loop like GIF on UIVIew

31 views Asked by At

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.

1

There are 1 answers

0
Shehata Gamal On

You need to comment this line

playerLayer.position = CGPoint(x: self.videoView.bounds.midX, y:self.videoView.bounds.midY)

As it makes the top left corner of playerLayer in the middle of the videoView