title: Error when create AAC encoder with MediaCodec
body: <p>The question is simple, But I have no any clue to solve it:</p>
  
  <p>I write a single line code to create an AAC encoder on my Nexus 4 (Android 4.4.2)</p>
  
  <pre><code>MediaCodec codec = MediaCodec.createEncoderByType("audio/mp4a-latm");
  </code></pre>
  
  <p>The return value saved in "codec" is not null, but I get a red error message in Logcat:</p>
  
  <pre><code>03-20 15:25:08.985: E/OMXMaster(24517): A component of name 'OMX.qcom.audio.decoder.aac' already exists, ignoring this one.
  </code></pre>
  
  <p>I have also tried another line:</p>
  
  <pre><code>MediaCodec codec = MediaCodec.createByCodecName("OMX.google.aac.encoder");
  </code></pre>
  
  <p>And get the same error result.</p>
  
  <p>Did I miss any initialization steps before using MediaCodec? I did not find any information about this in the official document.</p>
  
  <p>Did anyone run into this problem?</p>
  
  <p>Actually I am trying to encode PCM to AAC file. And I have read this <a href="https://stackoverflow.com/questions/18862715/how-to-generate-the-aac-adts-elementary-stream-with-android-mediacodec">post</a> @hubeir. It seems that he has made it work. I did the same thing:(1)setup the mediacodec and feed in PCM data to get encoded frame. To do that, I read the code from <a href="https://android.googlesource.com/platform/cts/+/jb-mr2-release/tests/tests/media/src/android/media/cts/EncodeDecodeTest.java" rel="nofollow noreferrer">cts</a> . Every encoded frame length is about 371-379. (2)Add adts header to the frame, then save to file. I have checked the head bit by bit, it is correct. But the file is still not playable. So I think maybe the error log is the problem.</p>
  
  <p>The following is my whole code, for reference:</p>
  
  <pre><code>MediaCodec codec = MediaCodec.createByCodecName("OMX.google.aac.encoder");      
  MediaFormat format = new MediaFormat();
      format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
      format.setInteger(MediaFormat.KEY_AAC_PROFILE, 
                        MediaCodecInfo.CodecProfileLevel.AACObjectELD);
      format.setInteger(MediaFormat.KEY_SAMPLE_RATE, nSamplerate);
      format.setInteger(MediaFormat.KEY_BIT_RATE, 128000);
      format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, nChannels);
  
      codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
      codec.start();
  
      ByteBuffer[] inputBuffers = codec.getInputBuffers();
      ByteBuffer[] outputBuffers = codec.getOutputBuffers();
  
      boolean bEndInput = false;
      boolean bEndOutput = false;
  
  while(true)
  {
      if (!bEndInput)
      {
          int inputBufferIndex = codec.dequeueInputBuffer(0);
          if (inputBufferIndex &gt;= 0)
          {
              int nLen = app.readPCM(nHandle,inputBuffers[inputBufferIndex]);//This line read PCM, return 0 if end of data.
              int nBufLen = inputBuffers[inputBufferIndex].capacity(); 
              if (nLen == nBufLen)
                  codec.queueInputBuffer(inputBufferIndex, 0, nLen, 0,  MediaCodec.BUFFER_FLAG_SYNC_FRAME);
              else if (nLen &lt; nBufLen)
              {
                  codec.queueInputBuffer(inputBufferIndex, 0, nLen, 0,  MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                  bEndInput = true;
                  break;
              }   
          }
      }
  
      MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
      if (!bEndOutput)
      {
              int outputBufferIndex = codec.dequeueOutputBuffer(info, 0);
              if (outputBufferIndex  &gt;= 0)
              {
                  int outBitsSize   = info.size;
                  Log.d("test", "Offset:"+info.offset);
                  Log.d("test", "Size:"+info.size);
                  Log.d("test", "Time:"+info.presentationTimeUs);
                  Log.d("test", "Flags:"+info.flags);
                  if (outBitsSize &lt;= 10)
                  {
                      codec.releaseOutputBuffer(outputBufferIndex, false /* render */);
                      continue;
                  }
  
                  int outPacketSize = outBitsSize + 7;    // 7 is ADTS size
                  ByteBuffer outBuf = outputBuffers[outputBufferIndex];
  
                  outBuf.position(info.offset);
                  outBuf.limit(info.offset + outBitsSize);
                  try {
                      byte[] data = new byte[outPacketSize];  //space for ADTS header included
                      addADTStoPacket(data, outPacketSize);
                      outBuf.get(data, 7, outBitsSize);
                      outBuf.position(info.offset);
                      outputStream.write(data, 0, outPacketSize);  //open FileOutputStream beforehand
                  } catch (IOException e) {
                      Log.e("test", "failed writing bitstream data to file");
                      e.printStackTrace();
                  }
  
                  outBuf.clear();
                  codec.releaseOutputBuffer(outputBufferIndex, false /* render */);
                  Log.d("test", "  dequeued " + outBitsSize + " bytes of output data.");
                  Log.d("test", "  wrote " + outPacketSize + " bytes into output file.");
  
                  if (info.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM)
                  {
                      bEndOutput = true;
                      //break;
                  }   
              }
              else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) 
              {
                   outputBuffers = codec.getOutputBuffers();
              } 
              else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) 
              {
  
              }
      }
  
      if (bEndInput &amp;&amp; bEndOutput)
          break;
  }
  </code></pre>