title: Android encoded stream (H264/AAC) audio does not play in Flash Player
body: <p>I'm working on adding a live broadcasting feature to an Android app. I do so through RTMP and make use of the <a href="https://github.com/dailymotion/dailymotion-sdk-android/tree/dev/sdk/src/main/java" rel="nofollow noreferrer">DailyMotion Android SDK</a>, which in turn makes use of <a href="https://github.com/Kickflip/kickflip-android-sdk" rel="nofollow noreferrer">Kickflip</a>.</p>
  
  <p>Everything works perfect, except for the playback of the audio on the website (which makes use of Flash). The audio does work in VLC, so it seems to be an issue with Flash being unable to decode the AAC audio.</p>
  
  <p>For the audio I instantiate an encoder with the "audio/mp4a-latm" mime type. The Android developer docs state the following about this mime type: "audio/mp4a-latm" - AAC audio (note, this is raw AAC packets, not packaged in LATM!). I expect that my problem lies here, but yet I have not been able to find a solution for it.</p>
  
  <p>Pretty much all my research, including <a href="https://stackoverflow.com/questions/18862715/how-to-generate-the-aac-adts-elementary-stream-with-android-mediacodec">this SO question</a> about the matter pointed me in the direction of adding an ADTS header to the audio byte array. That results in the following code in the writeSampleData method:</p>
  
  <pre><code>boolean isHeader = false;
  if ((bufferInfo.flags &amp; MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
      isHeader = true;
  } else {
      pts = bufferInfo.presentationTimeUs - mFirstPts;
  }
  
  if (mFirstPts != -1 &amp;&amp; pts &gt;= 0) {
      pts /= 1000;
  
      byte data[] = new byte[bufferInfo.size + 7];
      addADTStoPacket(data, bufferInfo.size + 7);
      encodedData.position(bufferInfo.offset);
      encodedData.get(data, 7, bufferInfo.size);
  
      addDataPacket(new AudioPacket(data, isHeader, pts, mAudioFirstByte));
  }
  </code></pre>
  
  <p>The addADTStoPacket method is identical to the one in the above mentioned SO post, but I will show it here regardless:</p>
  
  <pre><code>private void addADTStoPacket(byte[] packet, int packetLen) {
      int profile = 2;  //AAC LC
      //39=MediaCodecInfo.CodecProfileLevel.AACObjectELD;
      int freqIdx = 4;  //44.1KHz
      int chanCfg = 1;  //CPE
  
      // fill in ADTS data
      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>The variables in the above method match the settings I have configured in the application, so I'm pretty sure that's fine. </p>
  
  <p>The data is written to the output stream in the following method of the AudioPacket class:</p>
  
  <pre><code>@Override
  public void writePayload(OutputStream outputStream) throws IOException {
      outputStream.write(mFirstByte);
      outputStream.write(mIsAudioSpecificConfic ? 0 : 1);
      outputStream.write(mData);
  }
  </code></pre>
  
  <p>Am I missing something here? I could present more code if necessary, but I think this covers the most related parts. Thanks in advance and I really hope someone is able to help, I've been stuck for a couple of days now...</p>