title: AVAudioRecorder does not create .m4a files that can be opened and played
body: <p>I am trying to modify an iOS app that can send audio files (securely/encrypted) to other iOS devices.  It uses AVAudioRecorder to record the audio files, and it uses AVAudioPlayer to play back any received audio files.</p>
  
  <p>I am now trying to modify the app to create and send files that are compatible with Android.  I modified the existing AVAudioRecorder code to this:</p>
  
  <pre><code>[settings setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
  [settings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];
  [settings setValue:[NSNumber numberWithInt:16] forKey:AVEncoderBitRateKey];
  [settings setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
  [settings setValue:[NSNumber numberWithFloat:48000.0] forKey:AVSampleRateKey];
  
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *dirPath = [paths objectAtIndex:0];
  
  NSDateFormatter *format = [[NSDateFormatter alloc] init];
  [format setDateFormat:@"yyyy-MM-dd HH-mm"];
  
  NSString *filename = [NSString stringWithFormat:@"%@.aac", [format stringFromDate:NSDate.date]];
  </code></pre>
  
  <p>The files created by this are playable on a Mac, on an iOS device, but not on an Android device.  If the file extension were manually changed to end in ".m4a", then the file plays on Android.  </p>
  
  <p>(The advantage with the ".aac" format is that old versions of the app can receive these files and play them back — I've maintained backwards compatibility.)</p>
  
  <p>So I made this change:</p>
  
  <pre><code>NSString *filename = [NSString stringWithFormat:@"%@.m4a", [format stringFromDate:NSDate.date]];
  </code></pre>
  
  <p>However, the resultant file (which is created with no errors from AVAudioRecorder) cannot be played on Macs nor on iOS devices.  And I am waiting to hear whether it works on Android.</p>
  
  <p>When AVAudioPlayer tries to open that file, it outputs:</p>
  
  <blockquote>
    <p>Error Domain=NSOSStatusErrorDomain Code=1685348671 "The operation
    couldn’t be completed. (OSStatus error 1685348671.)" Code 1685348671
    -> "dta?": The file is malformed, not a valid instance of an audio file of its type, or not recognized as an audio file.</p>
  </blockquote>
  
  <p>Merely renaming the file to have a ".aac" again doesn't change the fact that it can't be loaded into anything on the Mac or iOS.</p>
  
  <p>Right now, it seems like the best solution would be to create a ".aac" file, and then have the Android end replace the ".aac" with ".m4a" and try to play it.  </p>
  
  <p>Question 1: Does anyone know why changing the filename extension can cause AVAudioPLayer to fail?  BTW, the ".m4a" file I created above, can't be opened with anything I have on the Mac so I can't tell whether the problem is with the file format or the inability of iOS or Macs to read this file type.</p>
  
  <p>For anyone curious, I put a copy of an ".aac" and a ".m4a" file from AVAudioRecorder, here:</p>
  
  <p><a href="https://dl.dropboxusercontent.com/u/14939586/2013-07-08%2013-24.m4a">https://dl.dropboxusercontent.com/u/14939586/2013-07-08%2013-24.m4a</a>
  <a href="https://dl.dropboxusercontent.com/u/14939586/2013-07-08%2010-11.aac">https://dl.dropboxusercontent.com/u/14939586/2013-07-08%2010-11.aac</a></p>
  
  <p>Question 2:  While I set AVAudioRecorder to one channel, the resultant file is stereo.  Why?  (At least that is what VLC says about the metadata)</p>
  
  <p>Question 3:  Is there anyway to specify AAC-LC for lossless encoding?  Or can I only do so if I move from AVAudioRecorder to a lower level API?</p>