How can I get the html5 audio's duration time

前端 未结 8 1515
忘掉有多难
忘掉有多难 2020-12-09 15:10

I have a html5 tag in page, but how can I know its duration time?

相关标签:
8条回答
  • 2020-12-09 15:24

    Simply use audioElement.duration

    0 讨论(0)
  • 2020-12-09 15:26
    var au = document.createElement('audio');
    au.addEventListener('loadedmetadata',function(){
        au.setAttribute('data-time',au.duration);
    },false);
    
    0 讨论(0)
  • 2020-12-09 15:30

    In a comment above, it was mentioned that the solution is to bind an event handle to the event loadedmetadata. This is how I did that -

    audio.onloadedmetadata = function() {
      alert(audio.duration);
    };
    
    0 讨论(0)
  • 2020-12-09 15:32

    To obtain the end of reading it is necessary that 'loop = false' with the event 'onended'. If loop = true, onended does not work;)

    To make a playlist, you have to set loop = false to use the 'onended' event in order to play the next song.

    for the duration if your script is done correctly you can recover the duration anywhere. If the value of 'duration' is NaN the file is not found by the browser. If the value is 'Infinity' 'INF' it is a streaming, in this case, adds 1 mn compared to the reading time 'currentime'. For * I.E it's crap. Currenttime may be greater than duration, in which case you do: var duration = (this.currentime> this.duration)? this.currenttime: this.duration;

    That's (O_ °)

    0 讨论(0)
  • 2020-12-09 15:34

    2020 solution:

    You will get undefined or NaN (not a number) when the audio metadata isn't loaded yet. Therefore some people suggested to use onloadedmetadata to make sure the metadata of the audio file is fetched first. Also, what most people didn't mention is that you have to target the first index of the audio DOM element with [0] like this:

    -- Vanilla Javascript:

    var audio = document.getElementById('audio-1');
    audio.onloadedmetadata = function() {
      alert(audio.duration);
    };
    

    If this won't work try this, however not so reliable and dependent on users connection:

    setTimeout(function () {
        var audio = document.getElementById('audio-1');
        console.log("audio", audio.duration);
    }, 100);
    

    -- JQuery:

    $(document).ready(function() {
       var audio = $("#audio-1")[0];
       $("#audio-1").on("loadedmetadata", function() {
           alert(audio.duration);
       }); 
    });
    
    0 讨论(0)
  • 2020-12-09 15:35

    I used "canplaythrough" event to get the track duration. I have a case where I have two players, and I want to stop the second player 2 seconds before the first one is complete.

    $('#' + _currentPlayerID).on("canplaythrough", function (e) {   
        var seconds = e.currentTarget.duration;    
        var trackmills = seconds * 1000;
        var subTimeout = trackmills - 2000; //2 seconds before end
    
        //console.log('seconds ' + seconds);
        //console.log('trackmills ' + trackmills);
        //console.log('subTimeout ' + subTimeout);
    
        //Stop playing before the end of thet track
        //clear this event in the Pause Event
        _player2TimeoutEvent = setTimeout(function () { pausePlayer2(); }, subTimeout);
    
    });
    
    0 讨论(0)
提交回复
热议问题