Html sound tag, duration is always infinity

I worked on using the html audio tag to play some audio files. The sound plays well, but the duration property of the sound tag always returns endlessly.

I tried the accepted answer to this question, but with the same result. Tested with Chrome, IE and Firefox.

Is this an error with the audio tag, or am I missing something.

Some of the code that I use to play audio files.

Javascript function when click playbutton

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 sound tag

<audio controls="true" id="audioplayerV2" style="display: none;" preload="auto">

Note: I am hiding the standard audio player with the intention of using a custom layout and using the player using javascript, this does not seem to be related to my problem.

+4
1

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

Source: https://habr.com/ru/post/1525007/


All Articles