Bryson Noble
Bryson Noble

Reputation: 351

Can you set the currentTime of audio in milliseconds instead of seconds? -Javascript

I am making something that requires audio to be set at an exact time. For this, I need to use currentTime in javascript or an equivalent (if any exist) to set the time in milliseconds.

This is my current code, which will set the audio to start 10 seconds in:

<audio id="sound" src="audio/example.mp3" preload loop></audio>
var snd = document.getElementById("sound");
snd.currentTime = 10;
snd.play();

I need something that would be able to set the time in millseconds instead of seconds if it is possible. Any help is appreciated.

Upvotes: 3

Views: 2210

Answers (1)

Veridian Dynamics
Veridian Dynamics

Reputation: 1406

var snd = document.getElementById("sound");
snd.currentTime = 0.001;
snd.play();

This will set the current time to 1/1000th of a second. However, the actually precision depends on the browser implementation and the device. So, you should use milliseconds to set your time, but you cannot guarantee it'll actually set the time to that precise index.

Upvotes: 3

Related Questions