0

I am attempting to make a streaming music player, but am having a bit of trouble with the SeekBar I am trying to implement. Currently, it acts correctly on the first song played. I can seek correctly and it updates itself in another thread. The problem I am having is that when a new song starts, the SeekBar flickers from the last songs duration to the current songs current progress. Any help would be appreciated.

SeekBar Declaration

    progress = (SeekBar) findViewById(R.id.seekBar1);
    progress.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            if(fromUser) {
                media.seekTo(progress);
            }
            else {                  
            }               
        }

        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

    });

SeekBar update in seperate Thread

int current = 0;
    int total = media.getDuration();
    progress.setMax(total);
    progress.setIndeterminate(false);

    while(media!=null && current < total){
        try {
            Thread.sleep(1000); 
            current = media.getCurrentPosition();
            progress.setProgress(current);
        } catch (Exception e) {
            e.printStackTrace();
        }
    };
tkosanke
  • 1
  • 1
  • Can you provide the full declaration of the Thread class and how its interacted with? Is media not being set to null somewhere? Have you verified that current is ever reaching the value of total? – cyngus Jul 31 '12 at 18:07

2 Answers2

2

You shouldn't be manipulating your Views directly from a separate Thread. Use View.post or runOnUiThread to update the View instead.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
0

It would seem like the thread you start to do the progress update never terminates, which means that it will keep on updating the progress once a second even after the song has ended.

current < total would most likely be the culprit here. From what I remember, current position will not (or is at least not guaranteed to) count all the way up to total length and thus your loop never terminates, you should instead add an OnCompletionListener to your MediaPlayer to get reliably notified when the song has ended.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294