title: Video Downloading in swift and play in AVPlayer in swift
body: <p>I am working in my to download video form the internet and play offline in AVPlayer but video is not playing</p>
  
  <p><a href="https://i.stack.imgur.com/Fxdf0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Fxdf0.png" alt="enter image description here"></a></p>
  
  <pre><code>session.downloadTask(with: request) { (loaction, respone, error) in
          if error != nil{
              print(error?.localizedDescription ?? "Error Found")
          }else if let res = respone as? HTTPURLResponse ,res.statusCode == 200{
              if let document = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first{
                  let desitnationurl = document.appendingPathExtension("mp4")
                  if FileManager.default.fileExists(atPath: desitnationurl.path){
                      //MarK: file is create but video cannot playing...
                      let ur = URL(fileURLWithPath: desitnationurl.path)
                      let pay = AVPlayer(url: ur)
                      let controller = AVPlayerViewController()
                      controller.player = pay
                      DispatchQueue.main.async {
                          self.present(controller, animated: true, completion: {
                              pay.play()
                          })
                      }
  
                  }else{
                      do{
                          try FileManager.default.copyItem(at: loaction!, to: desitnationurl)
                          self.URLoffile = desitnationurl.path
                          guard let ur = self.URLoffile else{
                              print("Error")
                              return}
                          print(ur)
  
                      }catch{
  </code></pre>