title: AVAudioRecorder record AAC/m4a
body: <p>I am trying to record audio on the Device using AVAudioRecorder. That file is transmitted to a web server and I want to play that resulting file in a web browser.. </p>
  
  <p>I have tried various combinations of settings on the device...nothing seems to encode the file into the correct AAC format.</p>
  
  <p>QuickTime says that it doesn't know how to play this file.</p>
  
  <p>Sample code</p>
  
  <pre><code>    private void InitializeRecordingSession() {
           string fileName = string.Format("{0}.m4a", Guid.NewGuid());
           string tmpdir = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/audio";
            if (!Directory.Exists(tmpdir))
                 Directory.CreateDirectory(tmpdir);
            audioFilePath = Path.Combine(tmpdir, fileName);
  
            Console.WriteLine("Audio File Path: " + audioFilePath);
  
            var url = NSUrl.FromFilename(audioFilePath);
            var settings = new AVAudioRecorderSettings() {
                 AudioFormat = AudioFormatType.MPEG4AAC,
                 SampleRate = 44100,
                 NumberChannels = 1,
                 AudioQuality = AVAudioQuality.High,    
            };
  
            recorder = AVAudioRecorder.ToUrl(url, settings, out error);
  
            //Set Recorder to Prepare To Record
            recorder.PrepareToRecord();  
       }
  </code></pre>
  
  <p><strong>Edit</strong>-By putting this code in before the line </p>
  
  <pre><code>recorder = AVAudioRecorder.ToUrl(url, settings, out error);
  </code></pre>
  
  <p>Everything worked.</p>
  
  <pre><code>NSError error;    
  AVAudioSession audioSession = AVAudioSession.SharedInstance();
  audioSession.SetCategory(AVAudioSession.CategoryRecord, out error);
  audioSession.SetActive(true, out error);
  </code></pre>