javascript事件触发多个声音

javascript事件触发多个声音,javascript,html,html5-audio,Javascript,Html,Html5 Audio,我正在编写一个简单的脚本,尝试使用js在html中放置2个或更多声音,并通过回调在某些事件(来自游戏)上激发它们 我设法让它与一个声音一起工作,但当我尝试添加第二个或多个声音时,它失败了,有什么想法吗 var audio = new Audio("audio.wav"); audio.addEventListener('ended', function() { this.currentTime = 0;

我正在编写一个简单的脚本,尝试使用js在html中放置2个或更多声音,并通过回调在某些事件(来自游戏)上激发它们

我设法让它与一个声音一起工作,但当我尝试添加第二个或多个声音时,它失败了,有什么想法吗

            var audio = new Audio("audio.wav");
        audio.addEventListener('ended', function()
        {
            this.currentTime = 0;
            this.play();
        }, false);
和回调

            {
            Start: function()
            {
                audio.play();
            },
            Stop: function()
            {
                audio.pause();
            },

        };

下面是一个根据我找到的教程改编的实现

// A sound pool to use for the sound effects.
// http://blog.sklambert.com/html5-canvas-game-html5-audio-and-finishing-touches/#adding-html-audio
function SoundPool(filename, volume, maxSize) {
    var pool = [];
    this.pool = pool;
    var currSound = 0;
    var that = this;

    // Populates the pool array with the given sound.
    for (var i = 0; i < maxSize; i++) {
        var sound = new Audio(filename);
        sound.volume = volume;
        pool[i] = sound;
    }

    this.setVolume = function setVolume(volume) {
        for (var i = 0; i < that.pool.length; i++) {
            that.pool[i].volume = volume;
        }
    };

    this.mute = function mute(state) {
        for (var i = 0; i < that.pool.length; i++) {
            // State: toggle, true or false.
            if (typeof state == "undefined")
                that.pool[i].muted = !that.pool[i].muted;
            else
                that.pool[i].muted = state;
        }
    };

    // Plays a sound.
    this.play = function () {
        if (pool[currSound].currentTime == 0 || pool[currSound].ended) {
            pool[currSound].play();
        }
        currSound = (currSound + 1) % maxSize;
    };
}

var laserSound = new SoundPool("sound/effects/laser.wav", 0.5, 300);

laserSound.play();
//用于声音效果的声音池。
// http://blog.sklambert.com/html5-canvas-game-html5-audio-and-finishing-touches/#adding-html音频
函数声音池(文件名、卷、最大大小){
var池=[];
this.pool=pool;
var=0;
var=这个;
//用给定的声音填充池数组。
对于(变量i=0;i
一次只能播放一种声音。您可以这样做: