Categories
JavaScript Answers

How to Set the HTML5 Audio Position with JavaScript?

Spread the love

To set the position of an HTML5 audio clip with JavaScript, we can set the currentTime property of the audio element.

For instance, we can write the following HTML:

<audio src='https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3' controls>
</audio>

to add the audio element with the controls displayed with the controls attribute.

Then we get the audio element and set the currentTime in seconds by writing:

const audio = document.querySelector('audio')
audio.currentTime = 10;

We select the audio element with document.querySelector .

Then we set the currentTime to 10 to move the time to 10 seconds.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.