How to set audio playing time to starting position in JavaScript

In this post, I will show you an easy way to set audio playing time to starting position in JavaScript. This is very much interesting and moreover, this post gonna be very useful for those who are building audio players or video players or working on the web application where HTML5 audio and video player are being used.

Here we gonna use currentTime feature of JavaScript.
I always say JavaScript is a very powerful tool to make web pages more user-friendly. So at the end of this tutorial, I will give you some tips on currentTime. So that you can use this feature to achieve some cool things that you can use on your projects.

How to Play Audio After Few Seconds or Delay in JavaScript

HTML5 Video/Audio player Volume Control With Key in JavaScript

Forward and backward HTML5 video player with left and right arrow key in JavaScript

Set audio playing time to starting point using JavaScript

function settime(){
	var audio= document.getElementById("myaudio");
	audio.currentTime=0;
	audio.play();
			
	}

audio.currentTime=0; This line is used to set the time to zero seconds.

If you wish to play the audio from 40th seconds then just set it to 40.

Below is the example of setting up the audio play to zero second



<!DOCTYPE html>
<html>
<head>
  <title>Audio time set to zero</title>
</head>
<body>
  <audio id="myaudio" src="filename.mp3"></audio>

  <button onclick="settime()">Set time to zero</button>
  <script type="text/javascript">
    
    function settime(){
      var audio= document.getElementById("myaudio");
      audio.currentTime=0;
      audio.play();
      
    }

  </script>
</body>
</html>

Now, if you realize the functionality of currentTime in JavaScript you can do cool things like

  1. Play audio from a particular time to a particular time. that means you can jump your audio start point.
  2. You can Create a button to set your audio to start point again.
  3. You can pause your audio after a certain period of time.

Bottom Sticky Music Player Source Code in JavaScript and CSS

 

Leave a Reply

Your email address will not be published. Required fields are marked *