title: AVAudioRecorder not saving recording
body: <p>I am making an iOS game. One of the things I need to do is to allow the user to make a quick little audio recording. This all works, but the recording is only temporarily saved. So when the user closes the app and reopens it, the recording should be able to play again, but it doesn't, it gets deleted when I close the app. I don't understand what I am doing wrong. Below is my code:</p>
  
  <p>I setup the <code>AVAudioRecorder</code> in the <code>ViewDidLoad</code> method like this:</p>
  
  <pre><code>// Setup audio recorder to save file.
  NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], @"MyAudioMemo.m4a", nil];
  NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
  
  // Setup audio session.
  AVAudioSession *session = [AVAudioSession sharedInstance];
  [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
  
  NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
  [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
  [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
  [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
  
  audio_recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
  audio_recorder.delegate = self;
  audio_recorder.meteringEnabled = YES;
  [audio_recorder prepareToRecord];
  </code></pre>
  
  <p>I have got the <code>AVAudio</code> delegate methods, too:</p>
  
  <pre><code>-(void)audio_playerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
      NSLog(@"Did finish playing: %d", flag);
  }
  
  -(void)audio_playerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error {
      NSLog(@"Decode Error occurred");
  }
  
  -(void)audio_recorderDidFinishRecording:(AVAudioPlayer *)recorder successfully:(BOOL)flag {
      NSLog(@"Did finish recording: %d", flag);
  }
  
  -(void)audio_recorderEncodeErrorDidOccur:(AVAudioPlayer *)recorder error:(NSError *)error {
      NSLog(@"Encode Error occurred");
  }
  </code></pre>
  
  <p>When I want to play, record or stop the audio, I have made the following IBActions which are linked to UIButtons:</p>
  
  <pre><code>-(IBAction)play_audio {
  
      NSLog(@"Play");
  
      if (!audio_recorder.recording){
          audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL:audio_recorder.url error:nil];
          [audio_player setDelegate:self];
          [audio_player play];
      }
  }
  
  -(IBAction)record_voice {
  
      NSLog(@"Record");
  
      if (!audio_recorder.recording) {
          AVAudioSession *session = [AVAudioSession sharedInstance];
          [session setActive:YES error:nil];
  
          // Start recording.
          [audio_recorder record];
      }
  
      else {
          // Pause recording.
          [audio_recorder pause];
      }
  }
  
  -(IBAction)stop_audio {
  
      NSLog(@"Stop");
  
      [audio_recorder stop];
  
      AVAudioSession *audioSession = [AVAudioSession sharedInstance];
      [audioSession setActive:NO error:nil];
  }
  </code></pre>
  
  <p>If you try my code you will see that it works, but it only seems to save the audio file temporarily.</p>
  
  <p>What am I doing wrong? I thought I had used all the correct <code>AVAudioRecorder</code> methods?</p>