title: How can I get AAC encoding with ExtAudioFile on iOS to work?
body: <p>I need to convert a WAVE file into an AAC encoded M4A file on iOS. I'm aware that AAC encoding is not supported on older devices or in the simulator. I'm testing that before I run the code. But I still can't get it to work.</p>
  
  <p>I looked into Apple's very own <a href="https://developer.apple.com/library/ios/#samplecode/iPhoneExtAudioFileConvertTest/Introduction/Intro.html" rel="noreferrer">iPhoneExtAudioFileConvertTest</a> example and I thought I followed it exactly, but still no luck!</p>
  
  <p>Currently, I get a -50 (= error in user parameter list) while trying to set the client format on the destination file. On the source file, it works.</p>
  
  <p>Below is my code. Any help is very much appreciated, thanks!</p>
  
  <pre><code>UInt32 size;
  
  // Open a source audio file.
  ExtAudioFileRef sourceAudioFile;
  ExtAudioFileOpenURL( (CFURLRef)sourceURL, &amp;sourceAudioFile );
  
  // Get the source data format
  AudioStreamBasicDescription sourceFormat;
  size = sizeof( sourceFormat );
  result = ExtAudioFileGetProperty( sourceAudioFile, kExtAudioFileProperty_FileDataFormat, &amp;size, &amp;sourceFormat );
  
  // Define the output format (AAC).
  AudioStreamBasicDescription outputFormat;
  outputFormat.mFormatID = kAudioFormatMPEG4AAC;
  outputFormat.mSampleRate = 44100;
  outputFormat.mChannelsPerFrame = 2;
  
  // Use AudioFormat API to fill out the rest of the description.
  size = sizeof( outputFormat );
  AudioFormatGetProperty( kAudioFormatProperty_FormatInfo, 0, NULL, &amp;size, &amp;outputFormat);
  
  // Make a destination audio file with this output format.
  ExtAudioFileRef destAudioFile;
  ExtAudioFileCreateWithURL( (CFURLRef)destURL, kAudioFileM4AType, &amp;outputFormat, NULL, kAudioFileFlags_EraseFile, &amp;destAudioFile );
  
   // Create canonical PCM client format.
  AudioStreamBasicDescription clientFormat;
  clientFormat.mSampleRate = sourceFormat.mSampleRate;
  clientFormat.mFormatID = kAudioFormatLinearPCM;
  clientFormat.mFormatFlags = kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;
  clientFormat.mChannelsPerFrame = 2;
  clientFormat.mBitsPerChannel = 16;
  clientFormat.mBytesPerFrame = 4;
  clientFormat.mBytesPerPacket = 4;
  clientFormat.mFramesPerPacket = 1;
  
  // Set the client format in source and destination file.
  size = sizeof( clientFormat );
  ExtAudioFileSetProperty( sourceAudioFile, kExtAudioFileProperty_ClientDataFormat, size, &amp;clientFormat );
  size = sizeof( clientFormat );
  ExtAudioFileSetProperty( destAudioFile, kExtAudioFileProperty_ClientDataFormat, size, &amp;clientFormat );
  
  // Make a buffer
  int bufferSizeInFrames = 8000;
  int bufferSize = ( bufferSizeInFrames * sourceFormat.mBytesPerFrame );
  UInt8 * buffer = (UInt8 *)malloc( bufferSize );
  AudioBufferList bufferList;
  bufferList.mNumberBuffers = 1;
  bufferList.mBuffers[0].mNumberChannels = clientFormat.mChannelsPerFrame;
  bufferList.mBuffers[0].mData = buffer;
  bufferList.mBuffers[0].mDataByteSize = ( bufferSize );
  
  while( TRUE )
  {
      // Try to fill the buffer to capacity.
      UInt32 framesRead = bufferSizeInFrames;
      ExtAudioFileRead( sourceAudioFile, &amp;framesRead, &amp;bufferList );
  
      // 0 frames read means EOF.
      if( framesRead == 0 )
          break;
  
      // Write.
      ExtAudioFileWrite( destAudioFile, framesRead, &amp;bufferList );
  }
  
  free( buffer );
  
  // Close the files.
  ExtAudioFileDispose( sourceAudioFile );
  ExtAudioFileDispose( destAudioFile );
  </code></pre>