How to get the size and duration of an mp3 file?

前端 未结 6 888
夕颜
夕颜 2021-01-12 07:13

I need to calculate the total length of an mp3 file.

Currently I am using a PHP class which I found @ http://www.zedwood.com/article/php-calculate-duration-of-mp3.

6条回答
  •  情话喂你
    2021-01-12 07:40

    Here's how you can get mp3 duration using Web Audio API:

    const mp3file = 'https://raw.githubusercontent.com/prof3ssorSt3v3/media-sample-files/master/doorbell.mp3'
    const audioContext = new (window.AudioContext || window.webkitAudioContext)()
    const request = new XMLHttpRequest()
    request.open('GET', mp3file, true)
    request.responseType = 'arraybuffer'
    request.onload = function() {
        audioContext.decodeAudioData(request.response,
            function(buffer) {
                let duration = buffer.duration
                console.log(duration)
                document.write(duration)
            }
        )
    }
    request.send()

提交回复
热议问题