VideoView持续时间

时间:2016-04-05 11:53:15

标签: android android-videoview android-seekbar

我正在使用videoview播放来自网址的视频。当我播放视频时,当前时间比视频的总持续时间长几秒钟。

04-05 17:20:27.457 9342-9342/exoplayer.com.videoview V/TOTAL: 00:45 04-05 17:20:27.557 9342-9342/exoplayer.com.videoview V/CURRENT: 00:46

 public String stringForTime(long timeMs) {
    mFormatBuilder = new StringBuilder();
    mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
    long totalSeconds = timeMs / 1000;
    int seconds = (int) (totalSeconds % 60);
    int minutes = (int) ((totalSeconds / 60) % 60);
    int hours = (int) (totalSeconds / 3600);
    mFormatBuilder.setLength(0);
    if (hours > 0) {
        return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
    } else {
        return mFormatter.format("%02d:%02d", minutes, seconds).toString();
    }
}

2 个答案:

答案 0 :(得分:1)

这不是答案。我只想告诉你如何做到这一点。

使用类似的东西:

public String milliSecondsToTimer(long milliseconds) {
    String finalTimerString = "";
    String secondsString = "";

    // Convert total duration into time
    int hours = (int) (milliseconds / (1000 * 60 * 60));
    int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
    int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
    // Add hours if there
    if (hours > 0) {
        finalTimerString = hours + ":";
    }

    // Prepending 0 to seconds if it is one digit
    if (seconds < 10) {
        secondsString = "0" + seconds;
    } else {
        secondsString = "" + seconds;
    }

    finalTimerString = finalTimerString + minutes + ":" + secondsString;

    // return timer string
    return finalTimerString;
}

请点击以下链接:

How to Display time of videoview in android

how to get total length of video in video view in android

答案 1 :(得分:0)

我是使用joda时间做的,可能有更好的方法,但我认为这比上面列出的解决方案更整洁

private String setTime(long elapsed) {
    final String PADDED_TIME_FORMAT = "00%s";
    final Period duration = new Period(elapsed);
    final String hours = String.format(PADDED_TIME_FORMAT, duration.getHours());
    final String minutes = String.format(PADDED_TIME_FORMAT, duration.getMinutes());
    final String seconds = String.format(PADDED_TIME_FORMAT, duration.getSeconds());
    return String.format("%s:%s:%s",
            hours.substring(hours.length() -2, hours.length()),
            minutes.substring(minutes.length() -2, minutes.length()),
            seconds.substring(seconds.length() -2, seconds.length()));
}