bCM
bCM

Reputation: 97

MediaPlayer convert from milliseconds to minutes and seconds

Class MediaPlayer have methods such as getCurrentPosition will return the value in milliseconds.

Heres the problem, when I get the value I use the class TimeUnit.MILLISECONDS.toSeconds and toMinutes, but I dont get the right values.

The values I is

xx:xzy and y is changing each seconds and when reaching 9 the z changes.

I need in xx:xx

Upvotes: 0

Views: 745

Answers (1)

showp1984
showp1984

Reputation: 378

This should work for you:

public static void main(String[] args) {
    int yourms = 3636600;
    int split[] = splitTime(yourms);
    System.out.println(yourms + " ms = " + split[0] + " minutes and " + split[1] + " seconds.");
}

public static int[] splitTime(int ms) {
    int ss = ms / 1000;
    int mm = ss / 60;
    ss = ss - (mm * 60);

    return new int[]{mm, ss};
}

Example Output:

3636600 ms = 60 minutes and 36 seconds.

Upvotes: 1

Related Questions