2

How to create a thread that will check the song position every second and move the seekbar based on the song position. i used the blow code it's play only two songs..then it's turn away from my applicatin

public class setp implements MediaPlayer.OnPreparedListener  
         {

            public void onPrepared(MediaPlayer mps) {
                // TODO Auto-generated method stub
                seekbar.setMax(mp.getDuration());
                 new Thread(new Runnable() {

                  public void run() {
                          while(mp!=null && mp.getCurrentPosition()<mp.getDuration())
                          {
                              seekbar.setProgress(mp.getCurrentPosition());
                              Message msg=new Message();
                              int millis = mp.getCurrentPosition();

                              msg.obj=millis/1000;

                               try {
                                   Thread.sleep(100);
                               } 
                               catch (InterruptedException e) {
                                  e.printStackTrace();
                               }
                          }
                  }
          }).start();
            }
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Palaniraja
  • 233
  • 9
  • 27

1 Answers1

2

the below coding is working fine

public class setp implements MediaPlayer.OnPreparedListener {

    public void onPrepared(MediaPlayer mps) {
        // TODO Auto-generated method stub
        seekbar.setMax(mp.getDuration());
        System.out.println("curpos" + mp.getCurrentPosition());
        new Thread(new Runnable() {
            public void run() {
                try {
                    while (mp != null && mp.getCurrentPosition() < mp.getDuration()) {
                        seekbar.setProgress(mp.getCurrentPosition());
                        Message msg = new Message();
                        int millis = mp.getCurrentPosition();

                        msg.obj = millis / 1000;

                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            System.out.println("interrupt exeption" + e);
                        }
                    } // end while
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("my Exception" + e);
                }
            }
        }).start();
    }
}
Sayed Abolfazl Fatemi
  • 3,678
  • 3
  • 36
  • 48
Palaniraja
  • 233
  • 9
  • 27