0

I have a ProgressBar in an app which takes a full second to update, but I am pretty sure the thread it is on is set up update every 100ms. I want the ProgressBar to update much quicker, maybe at the 100ms level or something smoother.

Relevant code:

/**
 * Update timer on seekbar
 * */
public void updateProgressBar() {
    mHandler.postDelayed(mUpdateTimeTask, 100);
}   

/**
 * Background Runnable thread
 * */
private Runnable mUpdateTimeTask = new Runnable() {

       public void run() {
           long totalDuration = mPlayer.getDuration();
           long currentDuration = mPlayer.getCurrentPosition();

           // Displaying Total Duration time
           soundTotalDurationTextView.setText(""+utils.milliSecondsToTimer(totalDuration));
           // Displaying time completed playing
           soundCurrentDurationTextView.setText(""+utils.milliSecondsToTimer(currentDuration));

           // Updating progress bar
           int progress = utils.getProgressPercentage(currentDuration, totalDuration);
           soundProgressBar.setProgress(progress);

           // Running this thread after 100 milliseconds
           mHandler.postDelayed(this, 100);
       }
};

/**
 * Function to get Progress percentage
 * @param currentDuration
 * @param totalDuration
 * */
public int getProgressPercentage(long currentDuration, long totalDuration){
    Double percentage = (double) 0;

    long currentSeconds = (int) (currentDuration / 1000);
    long totalSeconds = (int) (totalDuration / 1000);

    // calculating percentage
    percentage =(((double)currentSeconds)/totalSeconds)*100;

    // return percentage
    return percentage.intValue();
}
clever_trevor
  • 1,530
  • 2
  • 22
  • 42

3 Answers3

0

call the following code...

publishProgress(your time as integer);

eg:-publishProgress(100);

Hope it helps

0

I figured it out in case anyone ever needs this. I changed the divisor on currentSeconds and totalSeconds to 100, from 1000. This gave me a larger(and more precise) number.

 /**
 * Function to get Progress percentage
 * @param currentDuration
 * @param totalDuration
 * */
public int getProgressPercentage(long currentDuration, long totalDuration){
    Double percentage = (double) 0;

    long currentSeconds = (int) (currentDuration / 100);
    long totalSeconds = (int) (totalDuration / 100);

    // calculating percentage
    percentage =(((double)currentSeconds)/totalSeconds)*100;

    // return percentage
    return percentage.intValue();
}
clever_trevor
  • 1,530
  • 2
  • 22
  • 42
0

You can update it manually by yourself, using a Handler that you update it every X ms, setting how often you wish to do it.

Another approach is to use something like that (in case it's indeterminate and you don't mind using an ImageView that spins instead), that accelerates :

class MainActivity : AppCompatActivity() {
    private val handler = Handler()
    var curRotationIncrease = INITIAL_ROTATION_INCREASE
    var rotationIncreasePerFrame = INITIAL_ROTATION_INCREASE_PER_FRAME

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        curRotationIncrease = INITIAL_ROTATION_INCREASE
        rotationIncreasePerFrame = INITIAL_ROTATION_INCREASE_PER_FRAME
        val mSearchAnimationRunnable = object : Runnable {
            override fun run() {
                spinningImageView.rotation = spinningImageView.rotation + curRotationIncrease
                if (curRotationIncrease + rotationIncreasePerFrame < MAX_ROTATION_INCREASE)
                    curRotationIncrease += rotationIncreasePerFrame
                if (rotationIncreasePerFrame + ROTATION_INCREASE_INCREASE_PER_FRAME < MAX_ROTATION_INCREASE_PER_FRAME)
                    rotationIncreasePerFrame += ROTATION_INCREASE_INCREASE_PER_FRAME
                handler.postDelayed(this, FRAME_STEP_IN_MS)
            }
        }
        mSearchAnimationRunnable.run()
    }

    companion object {
        private const val INITIAL_ROTATION_INCREASE = 5f
        private const val INITIAL_ROTATION_INCREASE_PER_FRAME = 0.1f
        private const val TARGET_FPS = 60L
        private const val FRAME_STEP_IN_MS = 1000L / TARGET_FPS
        private const val ROTATION_INCREASE_INCREASE_PER_FRAME = 1f
        private const val MAX_ROTATION_INCREASE_PER_FRAME = 2f
        private const val MAX_ROTATION_INCREASE = 50f
    }
}
android developer
  • 114,585
  • 152
  • 739
  • 1,270