title: Seamless looping Video using AVPlayer- Swift
body: <p>I am using AVPlayer to play local video in background using loop and video is playing fine but after finishing video it takes pause to play video in loop. I have tried many methods and also seen many post on stack overflow but i failed to find appropriate solution. I am using Swift3.</p>
  
  <p>Code is here : </p>
  
  <pre><code>var videoplayer :AVPlayer = AVPlayer()
  
  override func viewDidLoad() {
  super.viewDidLoad()
  
  let path = Bundle.main.path(forResource: "background4", ofType: "mp4")
          videoplayer = AVPlayer(url: URL(fileURLWithPath: path!))
          videoplayer.volume = 0     
          videoplayer.actionAtItemEnd = AVPlayerActionAtItemEnd.none;
  
          let playerLayer = AVPlayerLayer(player: videoplayer)
          playerLayer.frame = self.view.frame
          playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
  
          if (videoplayer.rate != 0) {
              print("playing videoplayer")
              self.blurBgImage.isHidden = true
          }
  
          playerLayer.zPosition = -1
          videoplayer.rate = 0
          videoplayer.play()
          self.blurBgImage.layer.addSublayer(playerLayer)
  
   NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: videoplayer.currentItem, queue: nil, using: { (_) in
          DispatchQueue.main.async {
  
               let t1 = CMTimeMake(5, 100)
              self.videoplayer.seek(to: t1)
              self.videoplayer.play()
          }
      })
  }
  </code></pre>
  
  <p>I have also tried AVPlayerLooper. 
  Code is :   </p>
  
  <pre><code> var playerLooper: NSObject?
  var playerLayer:AVPlayerLayer!
  var queuePlayer: AVQueuePlayer?
  
   func playVideo(_ filmName: String){
         if let path = Bundle.main.path(forResource: filmName, ofType: "mp4")        let url =  URL(fileURLWithPath: path)
           if #available(tvOS 10.0, *) {
            let playerItem = AVPlayerItem(url: url as URL)
              self.videoplayer = AVQueuePlayer(items: [playerItem])
              self.playerLayer = AVPlayerLayer(player: self.videoplayer)
              self.playerLooper = AVPlayerLooper(player: self.videoplayer as! AVQueuePlayer, templateItem: playerItem)
              self.blurBgImage.layer.addSublayer(playerLayer!)
              self.playerLayer?.frame = self.view.frame
              self.videoplayer.volume = 10
              self.videoplayer.play()
          } else {
              videoplayer = AVPlayer(url: url)
              videoplayer.play()
              loopVideo(videoplayer)
          }
  }
  }
  </code></pre>
  
  <p>What should i do for seamless looping? Thanks in Advance.</p>