SoFunction
Updated on 2025-04-07

Use JavaScript implementation to get audio duration

When writing JS, I found a lot of audio acquisition time online but couldn't get it. Then I searched various tests and finally found it. It was easy to record it and use it later.

// A series of operations after the audio loading is completedfunction duration(){
	var myVid = ("videoDiv");
	("duration  ",myVid);
	if(myVid != null){
	     var duration;
	     ();
	      = function () {  
	            ("",);
				timeToMinute()
	      }
	}
}
 
// Convert the format of minutes in seconds to 00:00:00function timeToMinute(times){
       var t;
        if(times > -1){
            var hour = (times/3600);
            var min = (times/60) % 60;
            var sec = times % 60;
            if(hour < 10) {
                t = '0'+ hour + ":";
            } else {
                t = hour + ":";
            }
 
            if(min < 10){t += "0";}
            t += min + ":";
            if(sec < 10){t += "0";}
            t += (2);
        }
        t=(0,-3);
        return t;
  
}

Method supplement

js to get html5 audio audio duration method

var myVid = $('#' + showImgId)[0];
                                if (myVid != null) {
                                    var duration;
                                    ();
                                     = function () {
                                        ("", );
                                    }
                                }
var time = _audio.duration;
        //minute    var minute = time / 60;
    var minutes = parseInt(minute);
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    //Second    var second = time % 60;
    var seconds = (second);
    if (seconds < 10) {
        seconds = "0" + seconds;
    }

js get audio duration

The first type

var myVid = ("audio_duration");
("duration  ",myVid);
if(myVid != null){
     var duration;
     ();
      = function () {  
            ("",);
      }
}

The second type: There is a little error in this, because I use audio as a list

function getDuration() {
    setTimeout(function () {
    var duration = ;
    if(isNaN(duration)){
    getDuration();
    }else{
        var _duration = transTime();
        $(".audio_duration").html(_duration);
       ("Duration:"++"Second")
    }
    }, 10);
}

This is the article about using JavaScript to achieve audio duration. For more related content on JavaScript to obtain audio duration, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!