title: Problem with using ffmpeg on Android
body: <p>I am using ffmpeg on Android for play audio. But it doesn't work well.</p>
  
  <p>This is my Java code.</p>
  
  <pre><code>public class AudioPlayerActivity extends Activity 
  { 
  private AudioTrack track; 
  private FileOutputStream os; 
  /** Called when the activity is first created. */ 
  @Override 
  public void onCreate(Bundle savedInstanceState) 
  { 
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.main); 
  
      createEngine(); 
  
      //MediaPlayer mp = new MediaPlayer(); 
  
      //mp.start(); 
  
      int bufSize = AudioTrack.getMinBufferSize(44100, 
                                                AudioFormat.CHANNEL_CONFIGURATION_STEREO,  
                                                AudioFormat.ENCODING_PCM_16BIT); 
  
  
      track = new AudioTrack(AudioManager.STREAM_MUSIC,  
                             44100,  
                             AudioFormat.CHANNEL_CONFIGURATION_STEREO,  
                             AudioFormat.ENCODING_PCM_16BIT,  
                             bufSize, 
                             AudioTrack.MODE_STREAM); 
  
      byte[] bytes = new byte[bufSize]; 
  
      try { 
          os = new FileOutputStream("/mnt/sdcard/test.out",false); 
      } catch (FileNotFoundException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
      } 
  
      //loadFile("/mnt/sdcard/test.mp3", bytes);
      String file = "/mnt/sdcard/Wildlife.WMV";
      //String file = "/mnt/sdcard/test.mp3";
      loadFile(file, bytes);
  
      try { 
          os.close(); 
      } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
      } 
  
  } 
  
  void playSound(byte[] buf, int size) {   
      //android.util.Log.v("ROHAUPT", "RAH Playing"); 
      if(track.getPlayState()!=AudioTrack.PLAYSTATE_PLAYING) {   
          Log.d("test", "aaaaaaaaaaaaaaaaaaaaa");
          track.play();
          Log.d("test", "aaaaaaaaaaaaaaaaaaaaa");
      }
      track.write(buf, 0, size); 
  
      try { 
          os.write(buf,0,size); 
      } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
      } 
  } 
  
  
  private native void createEngine(); 
  private native int loadFile(String file, byte[] array); 
  
  /** Load jni .so on initialization*/  
  static { 
      System.loadLibrary("audioplayer");
  } 
  } 
  </code></pre>
  
  <p>And this is my C code.</p>
  
  <pre><code>jint Java_team_tj_audio_AudioPlayerActivity_loadFile(JNIEnv* env, jobject obj, jstring     file, jbyteArray array)
  {
  AVFormatContext *gFormatCtx = NULL;
  
  AVCodecContext *gAudioCodecCtx = NULL;
  AVCodec *gAudioCodec = NULL;
  int gAudioStreamIdx = -1;
  char *gAudioBuffer = NULL;
  
  int i, outsize = 0;
  AVPacket packet;
  PacketQueue audioq;
  
  const char *str;
  
  str = (*env)-&gt;GetStringUTFChars(env, file, NULL);
  
  jclass cls = (*env)-&gt;GetObjectClass(env, obj);
  
  jmethodID play = (*env)-&gt;GetMethodID(env, cls, "playSound", "([BI)V");
  
  if (gFormatCtx != NULL)
      return -1;
  
  if (av_open_input_file(&amp;gFormatCtx, str, NULL, 0, NULL) != 0)
      return -2;
  
  if (av_find_stream_info(gFormatCtx) &lt; 0)
      return -3;
  
  for (i = 0; i &lt; gFormatCtx-&gt;nb_streams; i++) {
      if (gFormatCtx-&gt;streams[i]-&gt;codec-&gt;codec_type == AVMEDIA_TYPE_AUDIO) {
          gAudioStreamIdx = i;
  
          break;
      }
  }
  
  if (gAudioStreamIdx == -1)
      return -4;
  
  gAudioCodecCtx = gFormatCtx-&gt;streams[gAudioStreamIdx]-&gt;codec;
  
  gAudioCodec = avcodec_find_decoder(gAudioCodecCtx-&gt;codec_id);
  
  if (gAudioCodec == NULL)
      return -5;
  
  if (avcodec_open(gAudioCodecCtx, gAudioCodec) &lt; 0)
      return -6;
  
  gAudioBuffer =  (char *) av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE * 2);
  
  while (av_read_frame(gFormatCtx, &amp;packet) &gt;= 0) {
      if (packet.stream_index == gAudioStreamIdx)
      {
          packet_queue_put(&amp;audioq, &amp;packet);
          int data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE * 2;
          int size=packet.size;
          while(size &gt; 0)
          {
              int len = avcodec_decode_audio3(gFormatCtx-&gt;streams[packet.stream_index]-&gt;codec,
                      (int16_t *) gAudioBuffer, &amp;data_size, &amp;packet);
              __android_log_print(ANDROID_LOG_INFO, "JNI", "bbbbbbbbbbbbbbbbb %d", len);
              size = packet.size-len;
              __android_log_print(ANDROID_LOG_INFO, "JNI", "bbbbbbbbbbbbbbbbb %d", packet.size);
              __android_log_print(ANDROID_LOG_INFO, "JNI", "bbbbbbbbbbbbbbbbb %d", data_size);
              if (data_size &gt; 0) {
                  jbyte *bytes = (*env)-&gt;GetByteArrayElements(env, array, NULL);
                  memcpy(bytes, gAudioBuffer, data_size); //
  
                  (*env)-&gt;ReleaseByteArrayElements(env, array, bytes, 0);
  
                  (*env)-&gt;CallVoidMethod(env, obj, play, array, data_size);
              }
  
          }
      }
      else
          av_free_packet(&amp;packet);
  }
  
  return 0;
  }
  </code></pre>
  
  <p>What I did wrong?</p>