title: Android How to set loop on AudioTrack?
body: <p>I'm playing .wav files using AudioTrack.
  I have a problem.
  I set the setLoopPoints to loop my .wav files, but it doesn't work.</p>
  
  <p>This is my sample code.</p>
  
  <pre><code>public class PleaseActivity extends Activity implements Runnable{
  AudioTrack audioTrack;
  public static final String MEDIA_PATH    = Environment.getExternalStorageDirectory().getAbsolutePath()+"/TEST";
  /** Called when the activity is first created. */
    Button play_button, stop_button;
    File file = null;
    byte[] byteData = null;
    Boolean playing = false;
    int bufSize;
    AudioTrack myAT = null;
    Thread play_thread = null;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      play_button = (Button) findViewById(R.id.btn1);
      stop_button = (Button) findViewById(R.id.btn2);
  
  
      file = new File(MEDIA_PATH+"/untitled1.wav");
      byteData = new byte[(int) file.length()];   
      FileInputStream in = null;
  
      try {
        in = new FileInputStream(file);
        in.read(byteData);
        in.close();
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
  
  
      initialize();
  
  
      play_button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
  
          play_thread.start();
        }
      });
  
      //
      stop_button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          //
          if (myAT.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
            myAT.stop();
            play_thread = null;
            initialize();
          }
        }
      });
  
    }
  
  
    void initialize() {
  
      bufSize = android.media.AudioTrack.getMinBufferSize(44100,
              AudioFormat.CHANNEL_CONFIGURATION_STEREO,
          AudioFormat.ENCODING_PCM_16BIT);
  
      myAT = new AudioTrack(AudioManager.STREAM_MUSIC,
          44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
          AudioFormat.ENCODING_PCM_16BIT, bufSize,
          AudioTrack.MODE_STREAM);
  
  
  
      play_thread = new Thread(this);
    }
  
  
    public void run() {
      if (myAT != null) {
  
        myAT.play();
        myAT.setLoopPoints(0, byteData.length, 2);
        myAT.write(byteData, 0, byteData.length);
  
  
      }
    }
  
  
  }
  </code></pre>
  
  <p>I can play my wave files well, but setLoopPoints doesn't work!
  Anybody helps me..</p>
  
  <hr>
  
  <p>I solved this looping problems like this.
  I have another problem.
  whenever I wrote data into audiotrack,
  I mean whenever audiotrack is repeated, some noise like "tick" is added at the first part.
  I don't know how to eliminate this noise..
  Is there anybody knows how to solve it?</p>
  
  <pre><code>class DLThread extends Thread
      {
          public void run() 
          {
              while(!DLThread.interrupted())
              {
                  if (myAT != null) {
                        //
  
                        myAT.play();
                        myAT.flush();
                        myAT.write(byteData, 0, byteData.length);
                      }
              }
  
  
          }
  
      }
  </code></pre>