synchronizing seekbar with the audio song

10,004

i have solved it. Create a thread that will check the song position every second and move the seekbar based on the song position

  mediaPos = mplayer.getCurrentPosition();
  mediaMax = mplayer.getDuration();

  seekbar.setMax(mediaMax); // Set the Maximum range of the
  seekbar.setProgress(mediaPos);// set current progress to song's

  handler.removeCallbacks(moveSeekBarThread);
  handler.postDelayed(moveSeekBarThread, 100); //cal the thread after 100 milliseconds


}
  /**
 * The Move seek bar. Thread to move seekbar based on the current position
 * of the song
 */
private Runnable moveSeekBarThread = new Runnable() {
    public void run() {
            if(mplayer.isPlaying()){

            int mediaPos_new = mPlayer.getCurrentPosition();
            int mediaMax_new = mPlayer.getDuration();
            seekbar.setMax(mediaMax_new);
            seekbar.setProgress(mediaPos_new);

            handler.postDelayed(this, 100); //Looping the thread after 0.1 second
    }

    }
};
Share:
10,004
John
Author by

John

Android, React and React-Native enthusiast , And code freak. Love to program and solve problems

Updated on June 04, 2022

Comments

  • John
    John almost 2 years

    I am playing a song from the sdcard.

    Its plays fine. I tried to implement the seekbar such that when the user moves the seekbar the song gets forwarded or rewnided as per the seek bar position.

    Here is the code

    public class ViewAudio extends Activity implements  OnSeekBarChangeListener,OnSeekCompleteListener,OnCompletionListener,OnPreparedListener{
     @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.audio_player);
    
            System.gc();
            Intent i = getIntent();
            Bundle extras = i.getExtras();
            filename = extras.getString("audiofilename");
    
            try {
                mPlayer.setDataSource(filename);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            try {
                mPlayer.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    
            mPlayer.setOnCompletionListener(this);
            mPlayer.setOnPreparedListener(this);
            mPlayer.setOnSeekCompleteListener((OnSeekCompleteListener) this);
    
            mPlayer.start();
    
            TextView tv=(TextView) findViewById(R.id.file);
            tv.setText("Playing "+filename);
    
            seekbar = (SeekBar)findViewById(R.id.seekBar1);
            mPlay = (Button)findViewById(R.id.play);
            mPause = (Button)findViewById(R.id.pause);
    
            seekbar.setProgress(0);
            seekbar.setOnSeekBarChangeListener(this);
            total = mPlayer.getDuration();
    
      }
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        Log.d("Progres changed",""+progress);
    
        if(fromUser){
            mPlayer.seekTo(progress);
            seekbar.setProgress(progress);
    
        }
    
    }
    
    }
    

    Now where ever i click the seekbar the song is getting played from the beginning not from the position i clicked. And it is not moving based on the song.

    Any one please help.