-1

I am trying to create a simple music player app. I want to display the current runtime of the song on the left and the remaining duration of the song to the right. In mm:ss format. I am invoking a thread which periodically updates the seekbar and the left/right times. the song is 4min 36 seconds long. So what I want to see is something like 0:00 in left and 4:36 in right in the beginning. Left time keeps on increasing by 1 second and simultaneously right decreases by 1 sec. but what I am seeing is 30:00 at left and 34:36 to right. An additional 30 mins are getting added to both the times initially. Why is this happening? Am I missing anything here?

public void updateThread(){
    thread = new Thread(){
        @Override
        public void run() {enter code here
            try{
                while(mediaPlayer != null && mediaPlayer.isPlaying()){
                    Thread.sleep(50);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), String.valueOf(mediaPlayer.getCurrentPosition()), Toast.LENGTH_LONG).show();
                            seekBar.setMax(mediaPlayer.getDuration());
                            seekBar.setProgress(mediaPlayer.getCurrentPosition());
                            leftTime.setText(String.valueOf(new SimpleDateFormat("m:ss").format(new Date(mediaPlayer.getCurrentPosition()))));
                            rightTime.setText(String.valueOf(new SimpleDateFormat("m:ss").format(new Date(mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition()))));
                        }
                    });
                }
            }
            catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    };
    thread.start();**
Jaymin
  • 2,879
  • 3
  • 19
  • 35
subu
  • 1
  • 1
  • You are using wrong format for minutes `m` is for Month as decimal (01 to 13) not minutes, use `mm` instead. – Omkar Jun 02 '18 at 13:18
  • Incorrect, @Omkar. Uppercase `M` is for month, Lowercase `m` works for minutes (and should be preferred over `mm` when single-digit minutes are possible, as in `4:36`). – Ole V.V. Jun 04 '18 at 08:04
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. For an amount of time the `Duration` class is the right one to use. – Ole V.V. Jun 04 '18 at 08:07

2 Answers2

2

Date(long date)

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

So, for example, new Date(1000) represents the date January 1, 1970, 00:00:01 GMT.

SimpleDateFormat uses the default timezone, unless specified otherwise. Are you in a time zone such as GMT+N:30? That's why you see 30 minutes added. Users in other time zones would see different output.

There's no reason to use Date/SimpleDateFormat here because you're not working with an actual date. Convert millis returned by getCurrentPosition manually. Here's an example.

Community
  • 1
  • 1
  • Thanks for answering! Ya my timezone is GMT + 5:30. I used TimeUnit class as u suggested and that is working! leftTime.setText(String.format("%d:%02d", TimeUnit.MILLISECONDS.toMinutes(currPos), TimeUnit.MILLISECONDS.toSeconds(currPos)%60)); rightTime.setText(String.format("%d:%02d", TimeUnit.MILLISECONDS.toMinutes(diff), TimeUnit.MILLISECONDS.toSeconds(diff)%60)); – subu Jun 02 '18 at 14:17
0

Set UTC as your formatter TimeZone:

mFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Hossein
  • 797
  • 1
  • 8
  • 24