我有一个问题。我正在尝试获取源的持续时间。
我想做下一步:当页面加载时,我需要获取源的持续时间,但问题是,当我想要获取持续时间时,显然不可见,只有当我按 a 时才能获取持续时间按钮。我需要知道自开始以来的持续时间,因为我需要计算视频/音频的位置并通过 currentTime 发送。
I try to do "alert" but the result is "NaN".
这实际上是我的代码:
$( document ).ready(function() {
var asset = $('#asset')[0]; // Obtiene el Objeto
var tipo = $('#asset').attr('class'); // Video, Audio, PDF
var duracion = asset.duration;
var porcentaje = $('#porcentaje').attr('data-percent');
var tiempo = (porcentaje*duracion)/100;
asset.currentTime = tiempo;
alert(duracion); // NaN
$("#guardar").click(function() {
var avance = asset.currentTime;
if(tipo == 'video' || tipo == 'audio'){
porcentaje = parseInt((avance*100)/duracion);
}
else if(tipo == 'pdf'){
porcentaje = 100;
}
alert(porcentaje);
});
});
仅此而已。谢谢。
古斯塔沃·G.
最佳答案
您至少必须等到元数据加载后才能获取视频时长,幸运的是有一个相关事件
$( document ).ready(function() {
var asset = $('#asset')[0]; // Obtiene el Objeto
var tipo = $('#asset').attr('class'); // Video, Audio, PDF
var duracion = 0;
var tiempo = 0;
var porcentaje = $('#porcentaje').data('percent');
asset.addEventListener('loadedmetadata', function() {
duracion = asset.duration;
tiempo = (porcentaje*duracion)/100;
asset.currentTime = tiempo;
});
$("#guardar").click(function() {
var avance = asset.currentTime;
if(tipo == 'video' || tipo == 'audio'){
porcentaje = parseInt((avance*100)/duracion);
} else if(tipo == 'pdf') {
porcentaje = 100;
}
});
});
关于javascript - 获取视频时长 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34620231/