繁体   English   中英

在HTML5中,我可以同时多次播放同一个声音吗?

[英]In HTML5, can I play the same sound more than once at the same time?

我正在制作一个游戏,其中经常播放声音。 我注意到声音在播放时不会再次播放。 例如,玩家与墙壁发生碰撞,播放“砰”的一声。 但是,如果玩家撞到一堵墙,然后又迅速撞到另一堵墙,只会播放“砰”的一声,我相信这是因为第一个声音没有结束。 真的吗? 我应该如何避免这种情况? 我想将声音预加载三次,总是播放那个声音的不同副本,但这似乎很愚蠢......

解决了:

事实证明我是对的......你需要预加载多个版本的声音然后循环播放它们。

代码:

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;
}

您可以克隆音频元素。 我不知道您是否可以同时播放多个声音,但是这是您可以多次播放声音而不下载一次的方法。

var sound = document.getElementById("incomingMessageSound")
var sound2 = sound.cloneNode()
sound.play()
sound2.play()

我知道这适用于chrome,这是我唯一需要的地方。 祝好运!

多次加载声音以模拟复音。 以循环方式播放它们。 matthewtoledo.com上查看我的演示。 具体来说,函数_initSoundBank()

是的,单个<audio>对象一次只能播放一个曲目。 请考虑<audio>对象具有currentTime属性,该属性指示音频轨道的当前位置:如果<audio>对象可以播放多个轨道一次,那么currentTime将反映什么值?

请参阅我的解决方案,以获取有关此问题的超集,有关Java演奏中的声音播放是否繁重? (基本上,添加具有相同声音的重复<audio>标签。)

尽管目前仅在FireFox和Chrome中实现,但将来您应该使用WebAudio API ,该API专为浏览器中的游戏和音频应用程序而设计。

您可以在那里多次播放任何声音,并用它来做其他有趣的事情。

一个很好的教程在这里: http : //www.html5rocks.com/en/tutorials/webaudio/games/

class SoundPool {
  pool : HTMLAudioElement[] = [];
  constructor(sound:HTMLAudioElement) {
    this.pool.push(sound);
  }

  play() {
    let found = false;
    for(var i=0; i<this.pool.length; i++) {
      if( this.pool[i].paused) {
        this.pool[i].play();
        found=true;
        break;
      }
    }
    if(!found) {
      let a = this.pool[0].cloneNode() as HTMLAudioElement;
      a.play();
      this.pool.push(a);
    }

  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM