Let users specify a minimum threshold for an audio asset to be considered loaded
Browsers don't give you an event when an audio item has completely loaded, but we can still manually compare the loaded time against what the user expects.
The upside is that you can then force Crafty.load to only continue when the full audio has been fetched.
The downside is that the user has to specify the expected audio length, and keep it up to date if the audio asset changes.
I realized that there's also a loadedmetadata event that fires when metadata, including the audio length, is available.
That means we could use a slightly more reasonable strategy: wait until both canplaythrough and length >= threshold || length >= duration are true. The threshold value would have a global default that's quite small, but a user could specify something much larger, both globally or for individual assets.
Good find!
The threshold value would have a global default that's quite small
Wouldn't it make sense to make the threshold = Infinity? Wait for it to be completely loaded, but have an option for advanced users, who understand the risk of starting the game too soon.