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

发布于 2024-10-29 22:57:50 字数 405 浏览 4 评论 0原文

你好 我想以简单的日期格式显示玩家位置和玩家持续时间。那是。 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());

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

请帮助我,斯瓦蒂·达鲁里。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蛮可爱 2024-11-05 22:57:50

DateFormat 适用于日期,不适用于时间间隔。因此,如果您获得 1 秒的位置,则 DateFormat 会将其解释为日期/时间是日历开始(即 1970 年 1 月 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));

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));
迷乱花海 2024-11-05 22:57:50

试试这个

fun getDurationString(durationMs: Long, negativePrefix: Boolean): String {
    val hours = TimeUnit.MILLISECONDS.toHours(durationMs)
    val minutes = TimeUnit.MILLISECONDS.toMinutes(durationMs)
    val seconds = TimeUnit.MILLISECONDS.toSeconds(durationMs)

    return if (hours > 0) {
        format(
            Locale.getDefault(), "%s%02d:%02d:%02d",
            if (negativePrefix) "- " else "",
            hours,
            minutes - TimeUnit.HOURS.toMinutes(hours),
            seconds - TimeUnit.MINUTES.toSeconds(minutes)
        )
    } else format(
        Locale.getDefault(), "%s%02d:%02d",
        if (negativePrefix) "- " else "",
        minutes,
        seconds - TimeUnit.MINUTES.toSeconds(minutes)
    )
}

just try this one

fun getDurationString(durationMs: Long, negativePrefix: Boolean): String {
    val hours = TimeUnit.MILLISECONDS.toHours(durationMs)
    val minutes = TimeUnit.MILLISECONDS.toMinutes(durationMs)
    val seconds = TimeUnit.MILLISECONDS.toSeconds(durationMs)

    return if (hours > 0) {
        format(
            Locale.getDefault(), "%s%02d:%02d:%02d",
            if (negativePrefix) "- " else "",
            hours,
            minutes - TimeUnit.HOURS.toMinutes(hours),
            seconds - TimeUnit.MINUTES.toSeconds(minutes)
        )
    } else format(
        Locale.getDefault(), "%s%02d:%02d",
        if (negativePrefix) "- " else "",
        minutes,
        seconds - TimeUnit.MINUTES.toSeconds(minutes)
    )
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文