我一直致力于使用 html 音频标签来播放一些音频文件。音频播放正常,但音频标签的持续时间属性总是返回无穷大。
我尝试了对 this 的可接受答案问题,但结果相同。已使用 Chrome、IE 和 Firefox 进行测试。
这是音频标签的错误,还是我遗漏了什么?
我用来播放音频文件的一些代码。
按下播放按钮时的javascript函数
function playPlayerV2(src) {
document.getElementById("audioplayerV2").addEventListener("loadedmetadata", function (_event) {
console.log(player.duration);
});
var player = document.getElementById("audioplayer");
player.src = "source";
player.load();
player.play();
}
html中的audio标签
<audio controls="true" id="audioplayerV2" style="display: none;" preload="auto">
注意:我隐藏了标准音频播放器,目的是使用自定义布局并通过 javascript 使用播放器,这似乎与我的问题无关。
最佳答案
试试这个
var getDuration = function (url, next) {
var _player = new Audio(url);
_player.addEventListener("durationchange", function (e) {
if (this.duration!=Infinity) {
var duration = this.duration
_player.remove();
next(duration);
};
}, false);
_player.load();
_player.currentTime = 24*60*60; //fake big time
_player.volume = 0;
_player.play();
//waiting...
};
getDuration ('/path/to/audio/file', function (duration) {
console.log(duration);
});
关于javascript - html 音频标签,持续时间总是无穷大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21522036/