VideoView 持续时间

VideoView time duration

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

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

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

使用这样的东西:

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

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

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