title: Need get correct AVI Player class to play all AVI video files inside android application
body: <p>I try to get correct AVI video player to play AVI file since my current AVI Player class looks like not work really good anymore.</p>
  
  <p>Some AVI files can play correctly, but the others can not.</p>
  
  <p>People who know which AVI Video player class play all avi files exactly,</p>
  
  <p>Please help me,</p>
  
  <p>Thank you,</p>
  
  <p>p/s : </p>
  
  <p>I don't want intent to 3rd application to play AVI file.</p>
  
  <p>Below codes is current I used to play AVI files:</p>
  
  <p><strong>AVI Player.java</strong>
      package runnable;</p>
  
  <pre><code>import java.io.BufferedInputStream;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.util.ArrayList;
  
  import iterface.IVideoSink;
  
  public class VideoPlayer implements Runnable {
  private final int FPS = 24;
  /**
   * String section
   */
  private boolean IS_ALIVE = true;
  private long LAST_FRAME_TIME;
  /**
   * Data section
   */
  private ArrayList&lt;IVideoSink&gt; mAlVideoSinks;
  /**
   * Others section
   */
  private BufferedInputStream mBufferedInputStream;
  
  public VideoPlayer(String filename) {
      mAlVideoSinks = new ArrayList&lt;IVideoSink&gt;();
  
      try {
          mBufferedInputStream = new BufferedInputStream(new FileInputStream(filename));
      } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
  
  }
  
  public void addVideoSink(IVideoSink videoSink) {
      synchronized (mAlVideoSinks) {
          mAlVideoSinks.add(videoSink);
      }
  }
  
  public void removeVideoSink(IVideoSink videoSink) {
      synchronized (mAlVideoSinks) {
          if (mAlVideoSinks.contains(videoSink))
              mAlVideoSinks.remove(videoSink);
      }
  }
  
  @Override
  public void run() {
      int count = 0;
  
      while (IS_ALIVE) {
          if (LAST_FRAME_TIME == 0) {
              LAST_FRAME_TIME = System.currentTimeMillis();
          }
  
          try {
              long currentTime = System.currentTimeMillis();
              if (currentTime - LAST_FRAME_TIME &lt; 1000 / FPS) {
                  Thread.sleep(1000 / FPS - (currentTime - LAST_FRAME_TIME));
              }
              LAST_FRAME_TIME = System.currentTimeMillis();
  
              int b0 = mBufferedInputStream.read();
              if (b0 == -1) break;
              int b1 = mBufferedInputStream.read();
              int b2 = mBufferedInputStream.read();
              int b3 = mBufferedInputStream.read();
  
              count = b0 + (b1 &lt;&lt; 8) + (b2 &lt;&lt; 16) + (b3 &lt;&lt; 24);
              byte[] buffer = new byte[count];
              int readCount = mBufferedInputStream.read(buffer, 0, count);
              for (IVideoSink videoSink : mAlVideoSinks) {
                  videoSink.onFrame(buffer, null);
              }
  
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  
      try {
          mBufferedInputStream.close();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      for (IVideoSink videoSink : mAlVideoSinks) {
          videoSink.onVideoEnd();
      }
  
  }
  
  public void stop() {
      IS_ALIVE = false;
  }
  
  }
  </code></pre>
  
  <p><strong>PCM Player.java</strong></p>
  
  <pre><code>package runnable;
  
  import android.media.AudioFormat;
  import android.media.AudioManager;
  import android.media.AudioTrack;
  
  import java.util.ArrayList;
  
  public class PCMPlayer implements Runnable {
  /**
   * String section
   */
  private boolean IS_ALIVE = true;
  /**
   * Data section
   */
  private ArrayList&lt;byte[]&gt; mAlBuffers = new ArrayList&lt;byte[]&gt;();
  /**
   * Other section
   */
  private AudioTrack mAudioTrack;
  
  public PCMPlayer() {
  }
  
  @Override
  public void run() {
      int bufSize = AudioTrack.getMinBufferSize(8000,
              AudioFormat.CHANNEL_CONFIGURATION_MONO,
              AudioFormat.ENCODING_PCM_16BIT);
      mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
              8000,
              AudioFormat.CHANNEL_CONFIGURATION_MONO,
              AudioFormat.ENCODING_PCM_16BIT,
              bufSize,
              AudioTrack.MODE_STREAM);
      mAudioTrack.play();
  
      while (IS_ALIVE) {
          byte[] buffer = null;
          boolean dataFlag = true;
  
          while (dataFlag) {
              synchronized (mAlBuffers) {
                  if (mAlBuffers.size() &gt; 0) {
                      buffer = mAlBuffers.remove(0);
                  } else {
                      dataFlag = false;
                      break;
                  }
              }
  
              mAudioTrack.write(buffer, 0, buffer.length);
          }
  
          try {
              Thread.sleep(10);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  
      mAudioTrack.stop();
      mAudioTrack.release();
  }
  
  public void writePCM(byte[] pcm) {
      synchronized (mAlBuffers) {
          byte[] buffer = new byte[pcm.length];
          System.arraycopy(pcm, 0, buffer, 0, buffer.length);
          mAlBuffers.add(buffer);
      }
  }
  
  public void stop() {
      IS_ALIVE = false;
  }
  }
  </code></pre>