Getting current playhead time in an interval using Web Audio

Viewed 65

I'm writing an app using Web Audio for playing different regions of an audio track as loops. Say I have one region between 1s and 4s and another between 10s and 14s. I want to user to live be able to jump between the loops.

How is this best done with best precision? What's the best practice for getting current time in an interval?

1 Answers

You can set an interval which observes the currentTime frequently:

const interval = setInterval(() => {
  if (baseAudioContext.currentTime < 10 && baseAudioContext.currentTime > 4) {
    // here you should seek to sec 1s
  }
  else if (baseAudioContext.currentTime > 14) {
    // here you should seek to sec 10s
  }
}, 20); // here you can change the interval

The important point is to find a proper place to clear this interval.

Related