我该如何正确显示的MediaPlayer的位置/多久? [英] How do I correctly display the position/duration of a MediaPlayer?

查看:180
本文介绍了我该如何正确显示的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.

下面是code我使用的:

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.

推荐答案

日期格式适用于日期,而不是​​时间间隔。所以,如果你得到1秒的位置,日期格式间$ P $点这是意义的日期/时间为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆