title: when i encode pcm to an aac file,the file is not correctly analyzed with aac analysis tool
body: <blockquote>
    <p>When the sample-rate is 44100,the aac byte array can be decoded and i
    can sound voice ,but others can not be decoded correctly.I don't know
    why?</p>
  </blockquote>
  
  <p>Before encode,i set the MediaFormat.the csd-0 is copy from other sample,and the MediaFormat param will be show.</p>
  
  <pre><code>private boolean prepare() {
      String codecName = null;
      for (int i = 0; i &lt; MediaCodecList.getCodecCount(); i++) {
          MediaCodecInfo mediaCodecInfo = MediaCodecList.getCodecInfoAt(i);
          for (String type : mediaCodecInfo.getSupportedTypes()) {
              if (TextUtils.equals(type, MIME_TYPE)
                      &amp;&amp; mediaCodecInfo.isEncoder()) {
                  codecName = mediaCodecInfo.getName();
                  break;
              }
          }
          if (null != codecName) {
              break;
          }
      }
      try {
          mBufferInfo = new MediaCodec.BufferInfo();
          mEncoder = MediaCodec.createByCodecName(codecName);
          MediaFormat mediaFormat = MediaFormat.createAudioFormat(MIME_TYPE,
                  KEY_SAMPLE_RATE, KEY_CHANNEL_COUNT);
          mediaFormat.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_AAC);
          mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, KEY_BIT_RATE);
          mediaFormat.setInteger(MediaFormat.KEY_AAC_PROFILE,
                  KEY_AAC_PROFILE);
          mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, mFrameSize);
          byte[] data = new byte[]{(byte) 0x11, (byte) 0x90};
          ByteBuffer csd_0 = ByteBuffer.wrap(data);
          mediaFormat.setByteBuffer("csd-0", csd_0);
          mEncoder.configure(mediaFormat, null, null,
                  MediaCodec.CONFIGURE_FLAG_ENCODE);
          mEncoder.start();
      } catch (IOException e) {
          e.printStackTrace();
          return false;
      }
      mBuffer = new byte[mFrameSize];
      int minBufferSize = AudioRecord.getMinBufferSize(KEY_SAMPLE_RATE, CHANNEL_IN,
              AUDIO_FORMAT);
      mRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
              KEY_SAMPLE_RATE, CHANNEL_IN, AUDIO_FORMAT, minBufferSize * 2);
      mRecord.startRecording();
      return true;
  }
  </code></pre>
  
  <p>Use android MediaCodec api to encode to aac in while.encoderCallback will be call to decode by MediaCodec.</p>
  
  <pre><code>long presentationTimeUs = 0;
      private void encode(byte[] data) {
          //api &gt; 21
          if (Build.VERSION.SDK_INT &gt; Build.VERSION_CODES.LOLLIPOP) {
  
              int inputBufferIndex = mEncoder.dequeueInputBuffer(-1);
              if (inputBufferIndex &gt;= 0) {
  
                  ByteBuffer inputBuffer = mEncoder.getInputBuffer(inputBufferIndex);
                  inputBuffer.clear();
  
                  inputBuffer.put(data);
                  inputBuffer.limit(data.length);
  
                  mEncoder.queueInputBuffer(inputBufferIndex, 0, data.length,
                          computePresentationTime(presentationTimeUs), BUFFER_FLAG_CODEC_CONFIG);
                  presentationTimeUs+=1;
              }
  
              int outputBufferIndex = mEncoder.dequeueOutputBuffer(mBufferInfo, 0);
  
              while (outputBufferIndex &gt;= 0) {
  
                  ByteBuffer outputBuffer = mEncoder.getOutputBuffer(outputBufferIndex);
  
                  outputBuffer.position(mBufferInfo.offset);
                  outputBuffer.limit(mBufferInfo.offset + mBufferInfo.size);
  
  
                  int length = mBufferInfo.size + 7;
                  if (mFrameByte == null || mFrameByte.length &lt; length) {
                      mFrameByte = new byte[length];
                  }
                  addADTStoPacket(mFrameByte, length);
  
                  outputBuffer.get(mFrameByte, 7, mBufferInfo.size);
  
                  if (encoderCallback!=null){
                      encoderCallback.encode(mFrameByte);
  
                      mFrameByte = null;
                  }
                  mEncoder.releaseOutputBuffer(outputBufferIndex, false);
                  outputBufferIndex = mEncoder.dequeueOutputBuffer(mBufferInfo, 0);
              }
      }
  </code></pre>
  
  <p>add adts header.</p>
  
  <pre><code>private void addADTStoPacket(byte[] packet, int packetLen) {
          int profile = KEY_AAC_PROFILE;  //AAC LC
          int freqIdx = FREQ_IDX;  //44.1KHz
          int chanCfg = CHAN_CFG;  //CPE
          packet[0] = (byte) 0xFF;
          packet[1] = (byte) 0xF9;
          packet[2] = (byte) (((profile - 1) &lt;&lt; 6) + (freqIdx &lt;&lt; 2) + (chanCfg &gt;&gt; 2));
          packet[3] = (byte) (((chanCfg &amp; 3) &lt;&lt; 6) + (packetLen &gt;&gt; 11));
          packet[4] = (byte) ((packetLen &amp; 0x7FF) &gt;&gt; 3);
          packet[5] = (byte) (((packetLen &amp; 7) &lt;&lt; 5) + 0x1F);
          packet[6] = (byte) 0xFC;
      }
  </code></pre>
  
  <p>These are the params about sample-rate.</p>
  
  <pre><code>public interface AudioCodec {
      String MIME_TYPE = MediaFormat.MIMETYPE_AUDIO_AAC;
      int KEY_CHANNEL_COUNT = 2;
      int KEY_BIT_RATE = 64 * 1024;
      int KEY_SAMPLE_RATE = 44100;
      int CHANNEL_OUT = AudioFormat.CHANNEL_OUT_STEREO;
      int CHANNEL_IN = AudioFormat.CHANNEL_IN_STEREO;
      int KEY_AAC_PROFILE = MediaCodecInfo.CodecProfileLevel.AACObjectLC;
      int WAIT_TIME = 10000;
      int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
      int BUFFFER_SIZE = 2048;
      int FREQ_IDX = 4;
      int CHAN_CFG = 1;
  }
  </code></pre>