html audio tag, duration always infinity

html, html5-audio, javascript

Solution

try this

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);
});

Problem

I've been working on using the html audio tag to play some audio files. The audio plays alright, but the duration property of the audio tag is always returning infinity. I tried the accepted answer to this question but with the same result. Tested with Chrome, IE and Firefox. Is this a bug with the audio tag, or am I missing something? Some of the code I'm using to play the audio files. javascript function when playbutton is pressed ``` 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(); } ``` the audio tag in html ``` <audio controls="true" id="audioplayerV2" style="display: none;" preload="auto"> ``` note: I'm hiding the standard audio player with the intend of using custom layout and make use of the player via javascript, this does not seem to be related to my problem.

Original source

Related problems