Set a time_callback for a negative time

I’d like to fire a callback 10 seconds from the end of a track. Is that possible, without knowing the length of the track up front?

Hey @drsm79! Thanks for reaching out. @danpastori’s might be a little delayed because his father unexpectedly passed away earlier this month. He’s been back with family and away from the keyboard as he helps pick up the pieces. You can read about it in detail here.

He’ll hopefully have some time to review your question soon!

So sorry to hear that. No worries & no rush. I’ll see if I can patch it in myself :slight_smile:

1 Like

@drsm79 This is a VERY interesting idea (negative callbacks). I’d like to explore this a little more. I don’t have a solution right now but this would be extremely useful.

I thought about it a bit more - the challenge I have is that the audio length is unknown, if you knew it it would be a pretty trivial bit of maths. What I’ve ended up doing is trusting the player to know the length & then done:

    my_amplitude.getAudio().ontimeupdate = function (e) {
      let duration = my_amplitude.getSongDuration()
      let position = my_amplitude.getSongPlayedSeconds()
      if (duration - position <= 10) { fire_callback()
    }

Which works, but feels a bit inelegant. I had problems getting the callbacks to bind as documented, hence the ontimeupdate - not sure if that was down to the bugs in the last release, user error or something else.

That’s a good solution! I’d love to build something into the library as well. I can see this being very useful for others.

As for the other callbacks, those are fixed in v5.3.0 which was launched Monday (Release v5.3.0 · serversideup/amplitudejs · GitHub)! Let me know if that helps!

Cool, I’ll try to rework it to use the callbacks over the weekend. You could do it by having an onplay or maybe durationchange callback which then creates or moves the negative callback…

If I init amplitude with no songs, and then add them later, it seems like the callbacks don’t work - am I doing that wrong?