Javascript DOM HTML Audio currentTime Property set

Introduction

Set time position to 1 second:

document.getElementById("myAudio").currentTime = 1;

View in separate window

<!DOCTYPE html> 
<html> 
<body> 

<audio id="myAudio" controls>
  <source src="sound.ogg" type="audio/ogg">
  <source src="sound.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio><br>
<p id="demo"></p>
<button onclick="getCurTime()" type="button">Get current time position</button>
<button onclick="setCurTime()" type="button">Set time position to 1 second</button>

<script>
var x = document.getElementById("myAudio");
function getCurTime() { //  w w w .j a  v a 2 s.c om
  document.getElementById("demo").innerHTML = x.currentTime;
} 

function setCurTime() { 
  x.currentTime = 1;
} 
</script> 

</body> 
</html>

The currentTime property sets or gets the current position in seconds of the audio.

Setting this property makes the playback to jump to the specified position.

The currentTime property returns a number representing the current play back time in seconds.




PreviousNext

Related