1.时间转化工具类,将mm转换为hh:mm:ss
背景:在Android播放器中,获取到的视频或音频资源的时长是mm显示的,为直观展示,需要进行转化。
工具类:
package com.example.checkbugs.Utils;
import java.text.SimpleDateFormat;
import java.util.Formatter;
/**
* 转换视频时长:mm -时:分:秒
*/
public class VideoDurationTools {
public static String transfMMtoNarmal(long mmTime){
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");//初始化Formatter的转换格式。
String hms = formatter.format(mmTime);
return hms;
}
public static String mm2hsm(long mmTime){
//1.小时
//sec=1000ms
//min = 60sec
//hour = 60min
int h = (int) (mmTime / (60*60*1000));
int min = (int) (mmTime%(60*60*1000) /(60*1000));
int sec = (int) ((mmTime%(60*60*1000) % (60*1000))/1000);
Formatter formatter = new Formatter();
String result = formatter.format("%02d:%02d:%02d", h, min, sec).toString();
return result;
}
}
调用:
int duration = video_view_player.getDuration();
int currentPosition = video_view_player.getCurrentPosition();
String curTime = VideoDurationTools.mm2hsm((long) currentPosition);
String durTime = VideoDurationTools.mm2hsm((long)duration);