Javascript DOM HTML Audio playbackRate Property set

Introduction

Play the audio in slow motion:

document.getElementById("myAudio").playbackRate = 0.5;

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="getPlaySpeed()" type="button">What is playback speed?</button>
<button onclick="setPlaySpeed()" type="button">Set audio to be play in slow motion</button>

<script>
var x = document.getElementById("myAudio");

function getPlaySpeed() { /*from  www  . ja va 2  s.c om*/
  document.getElementById("demo").innerHTML = x.playbackRate;
} 

function setPlaySpeed() { 
  x.playbackRate = 0.5;
} 
</script> 

</body> 
</html>

The playbackRate property sets or gets the current play speed of the audio.

Example values:

  • 1.0 is normal speed
  • 0.5 is half speed (slower)
  • 2.0 is double speed (faster)
  • -1.0 is backwards, normal speed
  • -0.5 is backwards, half speed



PreviousNext

Related