1

I am making an audio player and want to update seek bar while playing audio. I have searched online and written a method to update it using runOnUiThread but it stops the music while updating the seek bar. How to solve it apart from using services.

 getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                int duration = mp.getCurrentPosition() / 1000;
                                seekbar.setProgress(duration);
                                    mHandler.postDelayed(this, 1000);
                                }
                            });

3 Answers 3

1

you should do it like this

mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        int duration = mp.getCurrentPosition() / 1000;
        seekbar.setProgress(duration);
        mHandler.postDelayed(this,1000);
    }
},1000);
7
  • yes, it will only once, do you want it to run every 1 sec?
    – Rashid
    Jun 28, 2016 at 7:07
  • Yes, for every 1 sec.
    – mallaudin
    Jun 28, 2016 at 7:07
  • It is same. What's the difference?
    – mallaudin
    Jun 28, 2016 at 7:14
  • @mallaudin the above code looks working in your case, have a try on it
    – Rashid
    Jun 28, 2016 at 7:14
  • It is same. What's the difference?
    – mallaudin
    Jun 28, 2016 at 7:15
0

Try using Timer, example:

Timer timer = new Timer();
timer.schedule(new TimerTask() {
     @Override
     public void run() {
          int duration = mp.getCurrentPosition() / 1000;
          seekbar.setProgress(duration);
     }
}, 1000, 1000);

When you want cancel

timer.cancel();
timer = null;

More info about Timer

0

The problem is seekBar.setOnSeekBarChangeListener(this); So put it in Your AudioControl() Method..... You dont set oncheckchangedlistner to your seekbar....

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.