Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

android - How do I correctly display the position/duration of a MediaPlayer?

Hi I want to display player position and player duration in simple date format. that is. 00:00:01/00:00:06. The first part is current position of the player and the second part is the duration. I have used SimpleDateFormat to try display the duration and position in this format, but it is showing me the output as 05:30:00/05:30:06.

Here is the code I am using:

time1 = new SimpleDateFormat("HH:mm:ss");
currentTime.setText("" + time1.format(player.getCurrentPosition());

How do I print out the position and duration correctly? (It is displaying hours/minutes that should not be there).

Kindly help me out, Swathi Daruri.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

DateFormat works for dates, not for time intervals. So if you get a position of 1 second, the DateFormat interprets this as meaning that the date/time is 1 second after the beginning the calendar (which is January 1st, 1970).

You'd need to do something like

private String getTimeString(long millis) {
    StringBuffer buf = new StringBuffer();

    int hours = (int) (millis / (1000 * 60 * 60));
    int minutes = (int) ((millis % (1000 * 60 * 60)) / (1000 * 60));
    int seconds = (int) (((millis % (1000 * 60 * 60)) % (1000 * 60)) / 1000);

    buf
        .append(String.format("%02d", hours))
        .append(":")
        .append(String.format("%02d", minutes))
        .append(":")
        .append(String.format("%02d", seconds));

    return buf.toString();
}

And then do something like

totalTime.setText(getTimeString(duration));
currentTime.setText(getTimeString(position));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...