title: How can I use AVAssetReader and AVAssetWriter to create a AAC file?
body: <p>everyone!
  I'm dealing with an app.It can record audio file and mail to others.The audio file is saved as (.aac)file.Then I want to continue recording which I've already saved as .aac file.So I used  AVAssetReader and AVAssetWriter to make it.but I failed! The following is my code:</p>
  
  <pre><code>AVMutableComposition *mixComposition = [AVMutableComposition composition];
  NSURL *url = [NSURL fileURLWithPath:[DOCUMENTS_FOLDER stringByAppendingPathComponent:buttonTitle]];
  NSURL *anotherUrl = [NSURL fileURLWithPath:[DOCUMENTS_FOLDER stringByAppendingPathComponent:[fileList objectAtIndex:0]]];
  
  
  CMTime nextClipStartTime = kCMTimeZero;
  AVURLAsset *preAudioAsset = [[AVURLAsset alloc]initWithURL:url options:nil];
  CMTimeRange preTimeRange = CMTimeRangeMake(kCMTimeZero, preAudioAsset.duration);
  AVMutableCompositionTrack *preCompositonTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
  [preCompositonTrack insertTimeRange:preTimeRange ofTrack:[[preAudioAsset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0] atTime:nextClipStartTime error:nil];
  
  nextClipStartTime = CMTimeAdd(nextClipStartTime, preAudioAsset.duration);
  AVURLAsset *cruAudioAsset = [[AVURLAsset alloc]initWithURL:anotherUrl options:nil];
  CMTimeRange cruTimeRange = CMTimeRangeMake(kCMTimeZero, cruAudioAsset.duration);
  [preCompositonTrack insertTimeRange:cruTimeRange ofTrack:[[cruAudioAsset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0] atTime:nextClipStartTime error:nil];
  
  //AVAssetReader part
  NSError *error;
  AVAssetReader *assetReader = [[AVAssetReader assetReaderWithAsset:mixComposition error:&amp;error]retain];
  if(error)
  {
      NSLog(@"error:%@",error);
  
  }
  AVAssetTrack *audioTrack = [[mixComposition.tracks objectAtIndex:0]retain];
  AVAssetReaderOutput *assetReaderOutput =[[AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:audioTrack outputSettings:nil]retain];
  if(![assetReader canAddOutput:assetReaderOutput])
  {
      NSLog(@"Can't add output!");
      return;
  }
  [assetReader addOutput:assetReaderOutput];
  
  NSURL *exportUrl = [NSURL fileURLWithPath:[DOCUMENTS_FOLDER stringByAppendingPathComponent:@"combine.aac"]];
  
  //AVAssetWriter part
  AVAssetWriter *assetWriter = [[AVAssetWriter assetWriterWithURL:exportUrl fileType:AVFileTypeMPEG4 error:&amp;error]retain];
  if(error)
  {
      NSLog(@"error:%@",error);
      return;
  }
  AudioChannelLayout channelLayout;
  memset(&amp;channelLayout, 0, sizeof(AudioChannelLayout));
  channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
  NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                                  [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
                                  [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
                                  [ NSData dataWithBytes: &amp;channelLayout length: sizeof( AudioChannelLayout ) ], AVChannelLayoutKey,
                                  [ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey,
                                  nil];
  AVAssetWriterInput *assetWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:outputSettings]retain];
  if([assetWriter canAddInput:assetWriterInput])
  {
      [assetWriter addInput:assetWriterInput];
  }
  else
  {
      NSLog(@"can't add asset writer input .....");
      return;
  }
  assetWriterInput.expectsMediaDataInRealTime = NO;
  [assetWriter startWriting];
  [assetReader startReading];
  CMTime startTime = CMTimeMake(0, audioTrack.naturalTimeScale);
  [assetWriter startSessionAtSourceTime:startTime];
  
  dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue",NULL);
  [assetWriterInput requestMediaDataWhenReadyOnQueue:mediaInputQueue usingBlock:^ {
      while(assetWriterInput.readyForMoreMediaData)
      {
          CMSampleBufferRef nextBuffer = [assetReaderOutput copyNextSampleBuffer];
          if(nextBuffer)
          {
              //append buffer
              [assetWriterInput appendSampleBuffer:nextBuffer];
  
          }
          else
          {
              [assetWriterInput markAsFinished];
              [assetWriter finishWriting];
              [assetReader cancelReading];
              [assetReader release];
              [assetReaderOutput release];
              [assetWriter release];
              [assetWriterInput release];
              break;
          }
      }
  } ];
  </code></pre>
  
  <p>I just want to record another file and then append the later to the previous,but there is an error:-[AVAssetWriterInput appendSampleBuffer:] Input buffer must be in linear PCM format when outputSettings is not nil'</p>
  
  <p>I want to ask the methord I take is right? If I make it to PCM format(the .wav)it dong well ,but the size of the recorded file is too big.How to solve the error mentioned.And how can I
  convert .wav to .aac file by code?Help! </p>