0

I'm having a seeker bar for media player and I have implemented as below.

    //SeekBar setup     
    progress = (SeekBar)findViewById(R.id.seekBarPlayer);
    progress.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            if (fromUser) {
                mediaPlayer.seekTo(progress);
                Log.v(TAG,progress +"Seeking...");
            }

        }
    });

    //seekbar initiated after mediaplayer initiated
    total = mediaPlayer.getDuration();
    progress.setMax(total);

But my problem is seekBar doesn't run as the music plays. It idles at one place, if I seek then the music seeks but it doesn't run or make progress movment with the music. Can someone tell me what am I missing.

Thanks in advance for your time.

Jay Mayu
  • 17,023
  • 32
  • 114
  • 148
  • 1
    Check this post: http://stackoverflow.com/questions/2967337/android-how-do-i-use-the-progress-bar – Dimitris Makris Aug 29 '11 at 08:48
  • 1
    And where do you update it? I see the code for seekingTo, for getting max duration... and what about updating your seekbar progress manually from second thread? Please show it to us to let us make some conclusions. Check this out: http://stackoverflow.com/questions/2967337/android-how-do-i-use-the-progress-bar – Vivienne Fosh Aug 29 '11 at 08:49

2 Answers2

4

a thread is too heavy for this. here is a cut down version of what i use.

declare a handler in your activity

private boolean isPaused;
Handler handler = new Handler();

declare a runnable

    Runnable onEverySecond = new Runnable() {   

    @Override
    public void run() {
        if(!isPaused){
            progress.setProgress(player.getCurrentPosition());              

            handler.postDelayed(onEverySecond, EVERY_SECOND);
        }
    }
};

in your on resume

@Override
protected void onResume() {
    isPaused = false;
    handler.postDelayed(onEverySecond, EVERY_SECOND);
}

in your on pause

@Override
protected void onPause() {  
    isPaused = true;
    handler.removeCallbacks(onEverySecond);
}
Samuel
  • 9,883
  • 5
  • 45
  • 57
1

It is becoz you updated the progress bar in same thread in which media player runs. update your progress bar in UI (or different)thread.Look at this android: how do I use the progress bar?. Thnx.

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151