0

aud = document.getElementById('audio element');
btn = document.getElementById('btn element');

btn.addEventListener("click", playpause, false);

aud.oncanplay = reSync(1200);

function playpause() {
 if (aud.paused) {
  aud.play();
 } else {
  aud.pause();
 }
}

function reSync(time) {
  aud.currentTime = time;
  console.log(aud.currentTime);
}

I have a audio file I would like to play on my website. But for some reason when I try to change the current time to as an example to 1200 it become 900 and something instead. The different does seem dependent on how big a change I'm trying to do. I have no idea on how to fix this.

  • could you link the specific audio file and the code you use to change the time? – theusaf Oct 16 '19 at 00:29
  • is the audio really 20 minutes long? I wonder if the stream hasn't loaded enough to skip to that time – Bravo Oct 16 '19 at 00:39
  • this is the relevant parts of my code, and sorry but I don't own the audio but the file is a 35 MB aac file. – Erik Juel Jensen Oct 16 '19 at 00:41
  • you could use the `buffered` property to see if you are trying to seek outside of the available time range - https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/buffered - or the `seekable` property is probably more appropriate https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/seekable – Bravo Oct 16 '19 at 00:42
  • Refer [here](https://stackoverflow.com/questions/12029509/html-5-audio-play-file-at-certain-time-point), specifically `your script will first need to be after the audio tag.` as we cannot see your `HTML` – EGC Oct 16 '19 at 00:42
  • @EGC - in the question, it seems the seek does occur, just not to the point in time reuired – Bravo Oct 16 '19 at 00:44
  • yes Bravo the audio is 26 minutes long. – Erik Juel Jensen Oct 16 '19 at 00:45
  • There appears to be a bug: [HTML5 audio - currentTime attribute inaccurate?](https://stackoverflow.com/a/25468932/11700321) – EGC Oct 16 '19 at 00:47
  • the, as I suggested check the `buffered` and `seekable` properties – Bravo Oct 16 '19 at 00:47
  • @EGC - no such problem 5 years later :D – Bravo Oct 16 '19 at 00:50
  • Neat, okay, any chance it's [this](https://stackoverflow.com/a/42977670/11700321) ? `I had the same problem, and the reason was missing headers on the mp3 file, like: 'Content-Length, Content-Range, Content-Type'` – EGC Oct 16 '19 at 00:58
  • 1
    converting the file from a acc to ogg worked, thanks alot for the help u guys – Erik Juel Jensen Oct 16 '19 at 01:13

0 Answers0