How can I get the html5 audio's duration time

前端 未结 8 1534
忘掉有多难
忘掉有多难 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: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);
    
    });
    

提交回复
热议问题