我正在尝试以00:00:00格式从正在播放的Media Player中获取时长,分钟和秒。到目前为止,这就是我所拥有的。我几乎有,但不是100%。
public String convertDurationMillis(Integer getDurationInMillis){
int getDurationMillis = getDurationInMillis
String convertHours = String.format("%02d", TimeUnit.MILLISECONDS.toHours(getDurationMillis));
String convertMinutes = String.format("%02d", TimeUnit.MILLISECONDS.toMinutes(getDurationMillis));
String convertSeconds = String.format("%02d", TimeUnit.MILLISECONDS.toSeconds(getDurationMillis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(getDurationMillis)));
String getDuration = convertHours + ":" + convertMinutes + ":" + convertSeconds;
return getDuration;
}
这是我的textview
myTV.setText(convertDurationMillis(mediaPlayer.getDuration());
只要音频持续时间少于一小时,它就可以正常工作。如果音频持续时间等于或超过一个小时,这是我的结果... 02:149:51。我该如何纠正?预先感谢!
答案 0 :(得分:0)
没关系,我终于明白了!这就是答案...
public String convertDurationMillis(Integer getDurationInMillis){
int getDurationMillis = getDurationInMillis;
String convertHours = String.format("%02d", TimeUnit.MILLISECONDS.toHours(getDurationMillis));
String convertMinutes = String.format("%02d", TimeUnit.MILLISECONDS.toMinutes(getDurationMillis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(getDurationMillis))); //I needed to add this part.
String convertSeconds = String.format("%02d", TimeUnit.MILLISECONDS.toSeconds(getDurationMillis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(getDurationMillis)));
String getDuration = convertHours + ":" + convertMinutes + ":" + convertSeconds;
return getDuration;
}
谢谢大家!