0

Upong calling audioElement.duraion returns infinite in chromium based browsers but works find in firefox. The song is stored locally in the same directory.

console.log("Welcome to Spotify | clone");
let songIndex = 0;
const masterPlay = document.getElementById("masterPlay");
const progressBar = document.getElementById("progressBar");
const gif = document.getElementById("gif");
const audioElement=document.getElementById("song")

// let audioElement=new Audio('./1.mp3')
// Handeling play pause and click
masterPlay.addEventListener('click', () => {
    if (audioElement.paused || audioElement.currentTime <= 0) {
        audioElement.play();
        masterPlay.classList.remove("fa-circle-play");
        masterPlay.classList.add("fa-circle-pause");
        gif.style.opacity = 1;
    }
    else {
        audioElement.pause();
        masterPlay.classList.remove("fa-circle-pause");
        masterPlay.classList.add("fa-circle-play");
        gif.style.opacity = 0;

    }
});

//EventListeners
audioElement.addEventListener("timeupdate", () => {
    progress = ((audioElement.currentTime / audioElement.duration) * 100);
    progressBar.value = progress;
    console.log(audioElement.currentTime)
});

Expectations:

It returns the duration of the autio track.

What I tried:

  • Different browsers. (Works fine on firefox based browsers.)
  • Different song. (I found that some songs don't work on chrome but on firefox everything works.)
  • Tried using HTML audio element. (The problem persists)
  • Tried using JS audio(). (The problem still exists)
  • *If the element's media doesn't have a known duration—such as for live media streams—the value of duration is +Infinity* - is it a stream? – Konrad Jan 14 '23 at 15:55
  • It is a media from NCS called Mortals and it's around 3:50 sec and is stored in the same folder as the project files. Firefox works fine but every chromium based browser show problem – Debarka Mondal Jan 14 '23 at 15:59
  • So `progress` is `0` every time in chrome? – Konrad Jan 14 '23 at 16:01
  • Yes, exactly. It is always 0. – Debarka Mondal Jan 14 '23 at 16:04
  • Does this answer your question? [How can I get the html5 audio's duration time](https://stackoverflow.com/questions/11203773/how-can-i-get-the-html5-audios-duration-time) – Yogi Jan 14 '23 at 16:17
  • Are you running this from a web server or from the filesystem? I will suggest using a web server could help. – chrwahl Jan 14 '23 at 16:24
  • The problem is faced on chromium based browsers. I searched google but got no answer I read 2 stackoverflow threads too but I can't seem to get it working – Debarka Mondal Jan 14 '23 at 16:37
  • @Yogi Okay upon further testing some songs are not working but others are. On firefox everything is working but the things is why? – Debarka Mondal Jan 14 '23 at 16:48
  • The problem seems specific to your system. When I run your code in Chrome it works as expected with various mp3 files. I've not seen any documentation or posts about duration not working in Chrome. – Yogi Jan 14 '23 at 20:20
  • I also got it working but for some songs it doesn't work while upon changing currentTime it restets to 0 for some reason on chrome – Debarka Mondal Jan 14 '23 at 20:27
  • see related question: [HTML5 Audio Tag Showing Wrong Duration of MP3 in Chrome](https://stackoverflow.com/questions/20711488/html5-audio-tag-showing-wrong-duration-of-mp3-in-chrome) – Yogi Jan 15 '23 at 18:21

1 Answers1

0

At the end of the day I found out that it was a bug with VS code Live Preview extension from Microsoft

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 16 '23 at 15:41