如何获取 HTML5 音频播放器的播放时间?

How to get the played time from HTML5 audio player?

将一个简单的`HTML5 音频播放器视为 (live example)

<audio controls>
  <source src="https://hpr.dogphilosophy.net/test/flac.flac" type="audio/flac">
</audio>
<br />
<dic class="capture">
  Click here to get the current played time (elapsed time)
</dic>

我们可以创建一个 javascript 事件来通过单击 HTML 元素来捕获播放音频的经过时间吗?

我相信 HTML 音频有一个内置的 currentTime 属性,您可以使用它来获取音频中您当前所在的位置:

https://www.w3schools.com/tags/av_prop_currenttime.asp

这是您要找的吗?

使用currentTime获取当前时间戳,例如:

var audio = document.getElementById("audio-element");

document.getElementById('capture').addEventListener('click', () => {
    console.log(audio.currentTime);
});
<audio id="audio-element" controls>
  <source src="https://hpr.dogphilosophy.net/test/flac.flac" type="audio/flac">
</audio>
<br />
<button id="capture">
  Click here to get the current played time
</button>