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.
<!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>