Well which is it? Does your code? (or does your code -not- work?)
However. I think the main issues or at least first obstacle is going to be you detecting that ‘5 seconds’ before your audio clip ends. correct? (unless I’m reading things wrong?)
While this can certainly be done…it might not be very efficient, as you arent just using a built called back function that executes when the audio ends … you want to do -something- 5 seconds before hand. Which means you (very often) checking the state (currentTime) against the total length (duration) of the audio clip/object. (make sense?)
While this isnt exactly what you need/set up for your project. You should be able to check this out and pick out the key ‘ingredients’ that adapt for your needs:
I was initially thinking, you would be using setInterval() to check over and over the current states… but the more I think about it… perhaps using setTimeout() instead of a param of the duration - 5 seconds would work instead? (havent played with it myself…to be fair)
Actually here is an example what you want to do (I believe) (ignore the extra buttons after the intial ‘play button’… as they are just for extra info/learning.