Kann ich in HTML5 denselben Sound mehr als einmal gleichzeitig abspielen?

Ich mache ein Spiel, in dem oft Geräusche gespielt werden. Ich habe festgestellt, dass während der Wiedergabe kein Ton mehr abgespielt wird. Zum Beispiel kollidiert der Player mit einer Wand, ein "Schlag" wird abgespielt. Wenn der Player jedoch mit einer Wand kollidiert und dann schnell mit einer anderen Wand kollidiert, wird nur ein "Schlag" -Geräusch ausgegeben, und ich glaube, dass dies passiert, weil das erste Geräusch nicht beendet wurde. Ist das wahr? Wie soll ich das vermeiden? Ich dachte daran, den Sound dreimal vorzuladen und immer eine andere Kopie davon abzuspielen, aber das scheint ziemlich dumm zu sein ...

Gelöst:

Es stellte sich heraus, dass ich recht hatte ... Sie müssen mehrere Versionen des Sounds vorab laden und sie dann im Kreis abspielen.

DER CODE:

<code>var ns = 3; //The number of sounds to preload. This depends on how often the sounds need to be played, but if too big it will probably cause lond loading times.
var sounds = []; //This will be a matrix of all the sounds

for (i = 0; i < ns; i ++) //We need to have ns different copies of each sound, hence:
    sounds.push([]);

for (i = 0; i < soundSources.length; i ++)
    for (j = 0; j < ns; j ++)
        sounds[j].push(new Audio(sources[i])); //Assuming that you hold your sound sauces in a "sources" array, for example ["bla.wav", "smile.dog" "scream.wav"] 

var playing = []; //This will be our play index, so we know which version has been played the last.

for (i = 0; i < soundSources.length; i ++)
    playing[i] = 0; 

playSound = function(id, vol) //id in the sounds[i] array., vol is a real number in the [0, 1] interval
{
    if (vol <= 1 && vol >= 0)
        sounds[playing[id]][id].volume = vol;
    else
        sounds[playing[id]][id].volume = 1;

    sounds[playing[id]][id].play();
    ++ playing[id]; //Each time a sound is played, increment this so the next time that sound needs to be played, we play a different version of it,

    if (playing[id] >= ns)
        playing[id] = 0;
}
</code>

Antworten auf die Frage(4)

Ihre Antwort auf die Frage