HTML5 Game - Get Duration of audio or video file

Introduction

To get the length of the audio or video file, use the duration property.

It returns the duration in seconds.

If the audio or video element is streaming a file, the duration will be set as infinity.

Demo

ResultView the demo in separate window

<!DOCTYPE html>
<html>

  <body>
    <audio controls>
        <source src='http://java2s.com/style/demo/your.mp3' type='audio/mpeg'>
        <source src='http://java2s.com/style/demo/your.ogg' type='audio/ogg; codecs=vorbis'>
         //from  w  w w.  j  a v  a  2 s .  c  o m
    </audio>
    <script>
      let ele = document.querySelector('audio');
      //if you have a video tag
      // let ele = document.querySelector('video'); 
let ele = document.querySelector('audio'); 
console.log(ele.duration); 
      </script>
  </body>
</html>

Related Topic