如何正确显示 MediaPlayer 的位置/持续时间? [英] How do I correctly display the position/duration of a MediaPlayer?
问题描述
嗨我想以简单的日期格式显示玩家位置和玩家持续时间.那是.00:00:01/00:00:06
.第一部分是玩家的当前位置,第二部分是持续时间.我使用 SimpleDateFormat
尝试以这种格式显示持续时间和位置,但它显示输出为 05:30:00/05:30:06
.
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).
请帮帮我,Swathi Daruri.
Kindly help me out, Swathi Daruri.
推荐答案
DateFormat
适用于日期,不适用于时间间隔.因此,如果您获得 1 秒的位置,DateFormat 会将其解释为日期/时间是日历开始后 1 秒(即 1970 年 1 月 1 日).
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).
你需要做类似的事情
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();
}
然后做类似的事情
totalTime.setText(getTimeString(duration));
currentTime.setText(getTimeString(position));
这篇关于如何正确显示 MediaPlayer 的位置/持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!