title: Saving Recorded Audio (Swift)
body: <p>I'm trying to record an audio with AVAudioRecorder and then save it so i can use it later. So far I have Made the Recorder and it does the job well and plays the audio after recording but as soon as i close the program, it's gone. what can I do? Here's the entire code I have written so far :</p>
  
  <pre><code>class RecordViewController: UIViewController , AVAudioPlayerDelegate, AVAudioRecorderDelegate {
  
  @IBOutlet weak var RecordButton: UIButton!
  @IBOutlet weak var PlayButton: UIButton!
  
  
  var soundRecorder : AVAudioRecorder!
  var soundPlayer : AVAudioPlayer!
  var recordedAudio: RecordedAudio!
  var fileName = "audioFile.m4a"
  
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      setupRecorder()
  
  
      // Do any additional setup after loading the view.
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  
  func setupRecorder() {
  
  
      let recordSettings : [String : AnyObject] =
      [
          AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC),
          AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue as NSNumber,
          AVEncoderBitRateKey : 320000 as NSNumber,
          AVNumberOfChannelsKey: 2 as NSNumber,
          AVSampleRateKey : 44100.0 as NSNumber
      ]
  
      do {
  
        try   soundRecorder = AVAudioRecorder(URL: getFileURL(), settings: recordSettings)
              soundRecorder.delegate = self
              soundRecorder.prepareToRecord()
  
      } catch {
  
          print(error)
      }
  
  
  }
  
  func getCacheDirectory() -&gt; String {
  
      let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) 
  
      return paths[0]
  
  }
  
  func getFileURL() -&gt; NSURL{
  
      let path  = getCacheDirectory().stringByAppendingPathComponent(fileName)
  
      let filePath = NSURL(fileURLWithPath: path)
  
      return filePath
  }
  
  func preparePlayer() {
  
      do {
          try soundPlayer = AVAudioPlayer(contentsOfURL: getFileURL())
          soundPlayer.delegate = self
          soundPlayer.prepareToPlay()
          soundPlayer.volume = 1.0
      } catch {
          print(error)
      }
  
  }
  
  @IBAction func Record(sender: UIButton) {
  
  
      if sender.titleLabel?.text! == "Record" {
  
          soundRecorder.record()
          sender.setTitle("Stop", forState: .Normal)
          PlayButton.enabled = false
  
      } else {
  
          soundRecorder.stop()
          sender.setTitle("Record", forState: .Normal)
          PlayButton.enabled = false
  
      }
  
  }
  @IBAction func PlaySound(sender: UIButton) {
  
      if sender.titleLabel?.text! == "Play" {
  
          RecordButton.enabled = false
          sender.setTitle("Stop", forState: .Normal)
  
          preparePlayer()
          soundPlayer.play()
  
      } else {
  
          soundPlayer.stop()
          sender.setTitle("Play", forState: .Normal)
      }
  
  
  }
  
  
  
  func audioRecorderDidFinishRecording(recorder: AVAudioRecorder, successfully flag: Bool) {
      PlayButton.enabled = true
      recordedAudio = RecordedAudio()
      recordedAudio.filePathUrl = recorder.url
      recordedAudio.title = recorder.url.lastPathComponent
      print(recordedAudio.title)
  
  
  
  }
  func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
      RecordButton.enabled = true
      PlayButton.setTitle("Play", forState: .Normal)
  }
  </code></pre>