title: RAW PCM data encoded to m4a using FAAC is not recognizable?
body: <p>I'm trying to use FAAC through JNI to enable AAC encoding for my Android app project. Everything seems to be working fine, but the encoding part is, hmm... rather weird. I must admit that I'm not familiar with audio programming, and searched for solutions and answers for a few days already, but found no answer yet.</p>
  
  <p>The situation is, I recorded the audio into RAW PCM data using MediaRecord, and saved the file to a temp file, let's say, "temp.pcm". Then using the code below to encode it into AAC .m4a file. <strong>The problem is, the encoded file is saved, and the size looks fine, but can't be recognized by mPlayer or any other media player. Playing them will give some error like unsupported format.</strong> The encoded file seems like having some wrong structure.</p>
  
  <p>I don't have any clue on this. Anybody have tried this before? Share your experience or give some hint please... I'm so desperate on this... :(</p>
  
  <p>EDIT 1: Just thought, as the code below, am I actually getting the raw data of a m4a file, but having no header or other structures so that the players won't recognize it?</p>
  
  <p>The java part:</p>
  
  <pre><code>jint Java_com_phonegap_plugins_cjplugs_CJPlugs_JNIconvPCM2FAAC(
      JNIEnv* env,
      jobject thiz, 
      jstring inputPath, 
      jstring outputPath  )
  {
      const char *inFile = (*env)-&gt;GetStringUTFChars(env, inputPath, NULL);
      const char *outFile = (*env)-&gt;GetStringUTFChars(env, outputPath, NULL);
  
      return cppJNIconvPCM2FAAC(inFile, outFile);
  }
  </code></pre>
  
  <p>The actual JNI part in C++ bridged with another wrapper file:</p>
  
  <pre><code>#include &lt;cerrno&gt;
  #include &lt;cstddef&gt;
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;string.h&gt;
  #include &lt;math.h&gt;
  
  #include "ipaws.h"
  #include "faac.h"
  
  int cppJNIconvPCM2FAAC(
      const char *inputPath, 
      const char *outputPath  )
  {
      unsigned long faacInputSamples;
      unsigned long faacMaxOutputBytes;
  
      faacEncHandle faac = faacEncOpen(16000, 1, &amp;faacInputSamples, &amp;faacMaxOutputBytes);
      if ( !faac ) {
          return 0;
      }
  
      faacEncConfigurationPtr faacConfig = faacEncGetCurrentConfiguration(faac);
  
      faacConfig-&gt;mpegVersion   = MPEG4;
  //  faacConfig-&gt;aacObjectType = MAIN;
      faacConfig-&gt;aacObjectType = LOW;
      faacConfig-&gt;allowMidside  = 0;
      faacConfig-&gt;useLfe        = 0;
      faacConfig-&gt;useTns        = 0;
      faacConfig-&gt;bitRate       = 16000; // per channel
  //  faacConfig-&gt;quantqual     = 100;
      faacConfig-&gt;outputFormat  = 0;  // Raw
      faacConfig-&gt;inputFormat   = FAAC_INPUT_16BIT;
      faacConfig-&gt;bandWidth     = 0;
  
      if ( !faacEncSetConfiguration(faac, faacConfig) ) {
          return -1;
      }
  
      FILE* fd = fopen(inputPath, "rb");
      if ( fd == NULL ) {
          return -2;
      }
      FILE* fdout = fopen(outputPath, "wb+");
      if ( fdout == NULL ) {
          return -3;
      }
  
      char* bufSrc = new char[faacInputSamples*2];    // 每个采样16位PCM，2字节
      char* bufDst = new char[faacMaxOutputBytes];
  
      while ( 1 ) {
          int read = fread( bufSrc, faacInputSamples, 2, fd );
          if( read &lt; 1 )
              break;
          int nread = faacEncEncode(faac, (int32_t *)bufSrc, (unsigned int)faacInputSamples, (unsigned char*)bufDst, faacMaxOutputBytes);
  
          fwrite( bufDst, nread, 1, fdout );
      }
  
      fclose( fdout );
      fclose( fd );
      delete[] bufSrc;
      delete[] bufDst;
  
      faacEncClose( faac );
  
      return 1;
  }
  </code></pre>