android - 如何正确显示 MediaPlayer 的位置/持续时间?

嗨 我想以简单的日期格式显示玩家位置和玩家持续时间。那是。 00:00:01/00:00:06。第一部分是玩家的当前位置,第二部分是持续时间。 我使用 SimpleDateFormat 尝试以这种格式显示持续时间和位置,但它显示输出为 05:30:00/05:30:06

这是我正在使用的代码:

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

如何正确打印位置和持续时间? (它显示不应该存在的小时/分钟)。

请帮帮我,Swathi Daruri。

最佳答案

DateFormat 适用于日期,而不适用于时间间隔。因此,如果您获得 1 秒的位置,DateFormat 会将其解释为日期/时间是日历开始后 1 秒(即 1970 年 1 月 1 日)。

你需要做类似的事情

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));

https://stackoverflow.com/questions/5548922/

相关文章:

postgresql - 我可以在 PostgreSQL 中获得 CSV header 但没有行数吗

android - 在 EditText 中自动格式化电话号码

perl - 如何在 Perl 中输出文本表?

eclipse - 如何使 Eclipse Formatter 不破坏注释掉的代码?

javascript - Vue js 文本高亮过滤器

c# - Visual Studio 在 if 语句后添加空格

c# - microsoft.interop.excel 格式化单元格

python - python 3中的字符串拆分格式

r - 更改 R DataTables (DT) 中的字体大小

ide - 为什么 IDE 不支持动态格式化?