title: Save recording by AVAudioRecorder to Core data
body: <p>I'm trying to build a simple recording app that takes input from mic and saves the recording when you press stop, and the player retrieves the recording from local storage and plays it. </p>
  
  <p>The following code allows user to record audio and saves it to a URL and plays it from the same URL.</p>
  
  <p>What  I want to do is save audio to Coredata when I press stop, and retrieve audio from Coredata when I press play.</p>
  
  <pre><code>@IBOutlet weak var RecordBTN: UIButton!
  @IBOutlet weak var AddBTN: UIButton!
  
  // Variables handeling Recording and Playing
  var SoundRecorder : AVAudioRecorder!
  var SoundPlayer : AVAudioPlayer!
  
   override func viewDidLoad() {
      super.viewDidLoad()
      // Keeps constraints when we run the app
      self.view.layoutIfNeeded()
      setupRecorder() 
  }
  
  func setupRecorder()
  {
      // Format: Lossless audio
      // Quality: Max of raw value
      // Bit Rate: 320 Kb/s [ AppleMusic: 256 Kb/s, Spotify: 320 Kb/s]
      let recordSettings : [String : AnyObject] =
          [AVFormatIDKey : NSNumber(value: kAudioFormatAppleLossless as UInt32),
           AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue as NSNumber,
           AVEncoderBitRateKey : 320000 as NSNumber,
           AVNumberOfChannelsKey : 2 as NSNumber,
           AVSampleRateKey : 44100.0 as NSNumber]
  
      do {
  
          // Initiallizing SoundRecorder
          // as [NSObject : AnyObject] after recordSettings
          try SoundRecorder = AVAudioRecorder(url: getFileURL(),
                                              settings: recordSettings as [String : AnyObject])
  
      } catch {
          // Catches any error
          print("Something's went wrong with setting up the audio")
      }
  
      // Preparing sound recorder to record
      SoundRecorder.delegate = self
      SoundRecorder.prepareToRecord()
  
  
  }
  
  
  func getCacheDir() -&gt; String
  {
      // Where audio files will be held
      // Search inside documents inside of the app
      let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,
                                                      FileManager.SearchPathDomainMask.userDomainMask, true) as [String]
      return paths[0]
  }
  
  func getFileURL() -&gt; URL
  {
      // Finding the file path to the specified file name
      let path = (getCacheDir() as NSString).appendingPathComponent(fileName)
      let filePath = URL(fileURLWithPath: path)
  
      return filePath
  }
  
  
  func preparePlayer()
  {
      do{
          try SoundPlayer = AVAudioPlayer(contentsOf: getFileURL())
      } catch {
          print("Somethingwrong with the Sound Player")
      }
      SoundPlayer.delegate = self
      SoundPlayer.prepareToPlay()
      SoundPlayer.volume = 10.0
  }
  
  
  
  @IBAction func Record(_ sender: UIButton) {
      if (sender.titleLabel?.text == "Record")
      {
          SoundRecorder.record();
          sender.setTitle("Stop", for: UIControlState())
          sender.setImage(UIImage (named: "StopBTN"), for: UIControlState())
          PlayBTN.isEnabled = false
          progressBar.progress = Float(SoundRecorder.currentTime)
  
      }
      else
      {
          SoundRecorder.stop();
          sender.setTitle("Record", for: UIControlState())
          sender.setImage(UIImage (named: "RecordBTN"), for: UIControlState())
          PlayBTN.isEnabled = true
      }
  
  }
  
  
      @IBAction func Play(_ sender: UIButton) {
      if(sender.titleLabel?.text == "Play")
      {
          RecordBTN.isEnabled = false
          sender.setTitle("Stop", for: UIControlState())
          sender.setImage(UIImage (named: "StopBTN"), for: UIControlState())
          preparePlayer()
          SoundPlayer.play()
      }
      else if (sender.titleLabel?.text == "Stop" || (SoundPlayer.currentTime == SoundPlayer.duration))
      {
          SoundPlayer.stop()
          sender.setTitle("Play", for: UIControlState())
          sender.setImage(UIImage (named: "PlayBTN"), for: UIControlState())
          RecordBTN.isEnabled = true
      }
  }
  </code></pre>
  
  <p>I added a <code>Recordings</code> Entity in Coredata, still confused on what to use as attributes, don't know If I will save the URL or binaryfiles.</p>