Android Studio Progress Bar not updating correctly

171 views Asked by At

Currently Making a music player where I have a progress bar that tracks the current progress of the music playing. However I've tired many different approach and am unsure if its because the way I've done the thread or if its something else that may be really obvious and am not seeing it, the progress bar doesn't seem to want to track/set the progress Unless I pause the media file and continue playing. (EG. I start a mp3 file, music starts playing but progress bar isn't updating. I pause the music and press continue/ play again and then the progress bar would jump to the actual current progress of the music and actually continuously update)

Currently using a service to play the music and calling the service music player.

    public static BackgroundPlayer bp = new BackgroundPlayer();
    public static MP3Player player = bp.getPlayer();
    ProgressBar musicProgress;
    private String musicURL;
    private boolean isRunning;
    private float playbackspeed;

    private int progress;
    private int duration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_current_music);

        if(savedInstanceState != null)
        {
            progress = savedInstanceState.getInt("Progress");
            duration = savedInstanceState.getInt("Duration");
            isRunning = savedInstanceState.getBoolean("Running");
            playbackspeed = savedInstanceState.getFloat("SPEED");


            duration = player.getDuration();
            musicProgress.setMax(duration);

            if(isRunning)
            {
                new Thread( () -> {
                    while(isRunning)
                    {
                        progress = player.getProgress();
                        musicProgress.setProgress(progress);
                    }
                }).start();
            }

        }
    }


    public void onPlayClick (View v)
    {
        Intent intent = new Intent(CurrentMusic.this, BackgroundPlayer.class);
                player.stop();
                stopService(intent);
                startService(intent);
        duration = player.getDuration();
        musicProgress.setMax(duration);
        new Thread( () -> {
            while(isRunning)
            {
                progress = player.getProgress();
                musicProgress.setProgress(progress);
            }
        }).start();
    }

my background player is my service btw

2

There are 2 answers

0
P47HF1ND3R On BEST ANSWER

So the actual issue wasn't that the progress bar wasn't track. The progress bar it self tracks however for whatever reason when I first initially start the media player. It doesn't set the max progress hence progress bar cant show you the actual progress played since it doesn't know how to show progress / 0. But for whatever reason if I press pause and play again, it sets the max progress hence it can show the current progress relative to the total it needs to display.

I kinda just spam added set max method everywhere I could think it might be affecting it and deleted the ones that redundant

I also made a new method that uses a handler and updates the progress bar and in my thread it calls the method

4
Nazarii Moshenskyi On

The thing is you try to update musicPlayer (which is UI component) from non-UI thread. Only main (UI) thread can update UI.

Try wrapping updating logic with runOnUiThread():

new Thread( () -> {
    while(isRunning) {
        progress = player.getProgress();
        runOnUiThread(() -> musicProgress.setProgress(progress));
    }
}).start();

More details and other options to update UI are here: https://developer.android.com/guide/components/processes-and-threads#WorkerThreads

UPD: Also, you might be spamming UI thread with updating progress. Try to fetch and set progress once a second