3

hi i am new to Android programming i need little help in building a media player app in which i am using a seek bar to update the progress as below:

 Handler handler = new Handler();
    paly.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    s_player.start();
                    p_bar.run();
                }
            });

Runnable p_bar = new Runnable() {
        @Override
        public void run() {
            start_time = s_player.getCurrentPosition();
            s_bar.setProgress((int) start_time);
            handler.postDelayed(p_bar, 100);

        }
    };

so this code is updating sekkbar after 100ms, but the song is not playing smoothly???

3 Answers3

2

I've spotted a mistake here, it might be related to the problem, might not.

  Runnable p_bar = new Runnable() {
    @Override
    public void run() {
        start_time = s_player.getCurrentPosition();
        s_bar.setProgress((int) start_time);
        handler.postDelayed(p_bar, 100);

    }
};

in the p_bar you are calling the postDelayed within the run() method, while postDelay() will add the runnable object to the MessageQueue, and the run() method of the Runnable will be called by the System when this Runnable Object is reached in the Queue.

So it is like a loop, you can run, and postDelay will call run(), and run() will call postDelay() ....

So please don't call postDelay() in the run method.

Eric Liu
  • 1,516
  • 13
  • 19
2

Here is how i am doing;

paly.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    s_player.start();
                    handler.postDelayed(p_bar,100);

                }
            });

 Runnable p_bar = new Runnable() {
        @Override
        public void run() {
            s_bar.setProgress((int) s_player.getCurrentPosition());
        }
    };

now the problem is its playing smothly but not updating progress bar

0

You have to implement seekbar listener....and this works for me

    SeekBar.OnSeekBarChangeListener seekBarOnSeekChangeListener = new SeekBar.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) {
        // TODO Auto-generated method stub

        if (fromUser) {
            mediaPlayer.seekTo(progress);
            seekBar.setProgress(progress);
        }

    }
};
GvSharma
  • 2,632
  • 1
  • 24
  • 30
  • dear , i have already implemented onseekbarchangrdlistener and it is working nicely , the question is i want to continuesly update my seekbar progress with respect to song playing. – Mansoor Aziz Dec 28 '15 at 03:41