开发者

Android: Progress update SeekrBar

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 onStopTrackin开发者_开发百科gTouch(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.


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);
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜