0

我创建了一个音频播放器,并且我的项目中有一个用于处理持续时间的课程。如何确保 totalDuration 以相反的方向计算?

public class Utilities {
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;
}

public int getProgressPercentage(long currentDuration, long totalDuration){
    Double percentage = (double) 0;
    long currentSeconds = (int) (currentDuration / 1000);
    long totalSeconds = (int) (totalDuration / 1000);
    // calculating percentage
    percentage =((((double)currentSeconds)/totalSeconds)*100);
    // return percentage
    return percentage.intValue();
}

public int progressToTimer(int progress, int totalDuration) {
    int currentDuration = 0;
    totalDuration = (int) (totalDuration / 1000);
    currentDuration = (int) ((((double)progress) / 100) * totalDuration);
    // return current duration in milliseconds
    return currentDuration * 1000;
}
}
4

1 回答 1

0

很难弄清楚你想要做什么。也许您可以编辑您的问题以更好地解释?您是否希望 currentDuration 从 totalDuration 倒计时到 0?

我想你可能想要这个:

currentDuration = (int) totalDuration - ((((double)progress) / 100) * totalDuration);
于 2012-09-11T15:57:30.120 回答