js 根据音频/视频文件获取音频/视频时长
getDuration(file) {
let url = this.getSrc(file);
let audioElement = new Audio(url);
audioElement.addEventListener("loadedmetadata", () => {
var time = audioElement.duration;
//分钟
var minute = time / 60;
var minutes = parseInt(minute);
if (minutes < 10) {
minutes = "0" + minutes;
}
//秒
var second = time % 60;
var seconds = Math.floor(second);
if (seconds < 10) {
seconds = "0" + seconds;
}
file.duration = minutes + ":" + seconds;
this.$set(file, file)
});
},
只有Audio对象,没有Video对象,用Audio也可以获取video文件的时长
let u = navigator.userAgent;
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if (isAndroid) {
audio.oncanplay = () => {
duration.value = Math.ceil(audio.duration);
play();
};
}
if (isIOS) {
audio.load();
audio.onloadedmetadata= function() {
duration.value = Math.ceil(audio.duration);
play();
};
}