获取视频时长:
param.file:上传成功的文件
var url = URL.createObjectURL(param.file);
var audioElement = new Audio(url);
var duration=""
audioElement.addEventListener("loadedmetadata", function(_event) {
duration = audioElement.duration; //时长为秒,小数
});
时长转换成时分秒显示:
value:时长, dateFormat:显示格式
getDuration(value,dateFormat){
let hour = parseInt((value) / 3600);
if (hour<10) {//时
hour = "0" + hour;
}
var minute = parseInt((value % 3600) / 60);
if (minute<10) {//分
minute = "0" + minute;
}
var second = Math.ceil(value % 60);
if (second<10) {//秒
second = "0" + second;
}
switch (dateFormat) {
case 'hh:mm':
return hour + ":" + minute;
break;
case 'mm:ss':
return minute + ":" + second;
break;
case 'hh':
return hour;
break;
default:
return hour + ":" + minute + ":" + second;
break;
}
}
成功!