/**
* 取得 mp4的时长
*/
public static long getMp4Duration(File videoFile) {
if (videoFile == null || !videoFile.exists())
return 0;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
// source
try {
FileInputStream inputStream = new FileInputStream(videoFile.getAbsolutePath());
retriever.setDataSource(inputStream.getFD());
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInMillisec = Long.parseLong(time);
inputStream.close();
return timeInMillisec;
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
将其转化为时分秒
/**
* @author chenli
* @create 2020/2/18 0018
* @Describe
*/
public class TimeFormatUtil {
/**
* @param t 毫秒
*/
public static String format(long t) {
if (t < 60000) {
return (t % 60000) / 1000 + "秒";
} else if ((t >= 60000) && (t < 3600000)) {
return getString((t % 3600000) / 60000) + ":" + getString((t % 60000) / 1000);
} else {
return getString(t / 3600000) + ":" + getString((t % 3600000) / 60000) + ":" + getString((t % 60000) / 1000);
}
}
private static String getString(long t) {
String m = "";
if (t > 0) {
if (t < 10) {
m = "0" + t;
} else {
m = t + "";
}
} else {
m = "00";
}
return m;
}
}