[SOLVED] - How To Detect If An Audio File Has Been Loaded?

Hi,

We are stuck on something:
How can we detect if an audio file has been loaded?

We are currently rewriting the entire source code for image/audio loading…
Let us know, thanks!

Jesse

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
function LoadIndividualSound(index, path)
{
    TotalNumberOfSounds++;

    sleep(time*multiplier).then(() => {
        SoundArray[index] = document.createElement("Audio");
        SoundArray[index].src = path;

        SoundArray[index].preLoad = "auto";
    });

    multiplier++;
}

LoadIndividualSound(0, "./data/audio/effects/MenuClick.mp3");

We got it working:

//--------------------------------------------------------------------------------------------------------------
function CheckEffectLoaded()
{
    NumberOfLoadedSounds++;
}

//--------------------------------------------------------------------------------------------------------------
function LoadIndividualSound(index, path)
{
    TotalNumberOfSounds++;

    sleep(time*multiplier).then(() => {
        SoundArray[index] = document.createElement("Audio");
        SoundArray[index].src = path;

        SoundArray[index].preLoad = "auto";

        SoundArray[index].addEventListener("canplay", CheckEffectLoaded);
    });

    multiplier++;
}

feels somewhat unnecessary when you could just setTimeout directly here? All you’ve done is wrap setTimeout in a function called sleep :stuck_out_tongue:

Also i suspect you’re overengineering an array operation, but… eh. Beyond the scope of the thread.