Playing the same html audio multiple times before it ends

Viewed 151

I have a bunch of audio tags in my html, and was looking for a way to play the same sound multiple times. If a sound is still playing it cannot be triggered again.
I ended up making multiple tags for the same 2s sound bite, putting them in an array and cycling through them:

var sounds = [hit,hit2,hit3,hit4,hit5];

////////////

private soundIndex = 0
public playSoundFromArray(soundArray){
        if (this.soundIndex == soundArray.length -1){
            this.soundIndex = 0
        }
        else this.soundIndex++
        soundArray[this.soundIndex].play();
    }

That worked fairly well. Just after I'd finished that, I found a post dealing with the issue: HTML5 JS play same sound multiple times at the same time

The voted answer there was advising making and playing a clone of the audio every time:

sound.cloneNode(true).play();

This is great, and doesn't require any additional audio tags. It just felt a bit weird cloning nodes potentially hundreds of times, every time a sound is played. Is there any reason not to use this? Would the mass cloning affect the memory at all if they are not being saved as variables?

0 Answers
Related