DEV Community


Does anyone knows how to change current time of song correctly in JavaScript?

Pavel Keyzik
Front-end developer. Two things that I love most is React and automotive photography.
・1 min read

Hi, everyone! I'm trying to make custom audio player in JavaScript and when I try to change current time of song then on some songs it sets currentTime to 0. I tried to find solution and it seems like bug in Chrome or something. Maybe someone know how to fix it?

My code is so simple:

const audio = new Audio();

function playSong(songURL) {
  audio.src = songURL;;

function seek(value) {
  audio.currentTime = value;
Enter fullscreen mode Exit fullscreen mode

Discussion (2)

antopiras89 profile image
Antonio Piras • Edited

This code is perfectly fine:
I ran into this bug some time ago and it may be caused by one or more of the following reasons:

  1. You're trying to "seek" the track before it's loaded
  2. The headers for the file are not set correctly (
  3. Sometimes Chrome simply f****s up and it needs to be killed and restarted :)

Hope this helps.

pavelkeyzik profile image
Pavel Keyzik Author

Thank you, Antonio! It helped me a lot and I fixed the problem by providing correct headers.