title: How to Play Audio from Local File?
body: <p>I want to play a downloaded local audio file but it doesn't play:</p>
  
  <pre><code>class AVPlayerService {
  static let instance = AVPlayerService()
  
  private var audioPlayer: AVPlayer!
  public weak var delegate: AVPlayerServiceDelegate?
  
  func setupPlayer(forURL url: URL) {
      let playerItem: AVPlayerItem = AVPlayerItem(url: URL(fileURLWithPath: "\(url)")
      audioPlayer = AVPlayer(playerItem: playerItem)
      audioPlayer.play() 
  }
  </code></pre>
  
  <p>I get the local file here and then i call it in my viewcontroller:</p>
  
  <pre><code>func getDownloadedSurahFor(surah: Surah, reciter: Reciter) -&gt; (Exists, URL?) {
      let documentsDirectoryURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
  
      // lets create your destination file url
      let destinationUrl = documentsDirectoryURL.appendingPathComponent("\(reciter.name): \(surah.number)")
      print(destinationUrl)
  
      // to check if it exists before downloading it
      if FileManager.default.fileExists(atPath: destinationUrl.path) {
          print("Already downloaded")
          return (.exists, destinationUrl)
      } else {
          return (.doesNotExist, nil)
      }
  }
  </code></pre>
  
  <p>In my Viewcontroller i check if its exists and then i call setupPlayer:</p>
  
  <pre><code>let (exists, url) =  DataService.instance.getDownloadedSurahFor(surah: playingSurah, reciter: reciter)
          if exists == .exists {
              self.audioService.setupPlayer(forURL: url!)
              self.setupSlider()
              self.changeButtonTo(.pause)
              print("Downloaded file should now play")
          }
  </code></pre>