-1

I am setting an Audio element's currentTime = 0, but it always emit the audio ended event, and currentTime is always equal to the duration.

audio.addEventListener('loadedmetadata', (e) => {
  const that = this;
  const audio = this.oAudio;
  const duration = audio.duration;
  if (duration === Infinity) {
    audio.currentTime = 1e101;
    audio.ontimeupdate = function() {
      audio.ontimeupdate = () => {

      };
      audio.currentTime = 0;
      that.duration();
    };
  }
}, false);
Kaiido
  • 123,334
  • 13
  • 219
  • 285
mr.zx
  • 1
  • 1

1 Answers1

0

Because your Media lasts less than 1e+101 seconds.

I'm not entirely sure what it is you are trying to do here, nor why it is a problem that this ended event fires, but it sounds like you are trying to apply this workaround to get the correct duration of some medias.

If it is the case, then you would have to wait until you received that duration before attaching the ended event (and even before doing anything else with that MediaElement).

as pseudo-code that would be

// getMediaDuration is an asynchronous task
const duration = await getMediaDuration(audio);
// now that the async part is done we can add our listeners
audio.addEventListener('ended', dosomething);
Kaiido
  • 123,334
  • 13
  • 219
  • 285