HTML5 JS同时播放多次相同的声音 [英] HTML5 JS play same sound multiple times at the same time

查看:58
本文介绍了HTML5 JS同时播放多次相同的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个非常基本的HTML5游戏,并且碰壁了一点,基本上我有一个数组,这个数组中填充了一个声音文件(这是一个数组,因此如有需要,我可以分配更多的声音)

I'm create a very basic HTML5 game and I've hit a little wall, basically I have an array, this array is populated with one sound file (it is an array so I can assign more sounds if needed)

this.Sounds["L_Explosion1"] = new Audio("Assets/Sounds/Explosion_1.wav");
this.Sounds["L_Explosion1"].volume = 1;
this.Sounds["L_Explosion1"].load();

当播放器按下空格键时,我想播放声音,所以我运行这段代码

When the player presses the space bar I want to play the sound so I run this code

this.Sounds["L_Explosion1"].play();

但是,如果我在播放声音之前再按一次空格键将不会播放它,我想做的是多次播放同一声音(如果尚未完成相同的声音),我将如何实现呢?

however if I press space bar again before the sound played it will not play it, what I am trying to do is play the same sound multiple times event if the same has not yet finished, how would I achieve this?

推荐答案

为了同时播放声音的多个实例,您将需要创建该 Audio 元素的新副本..一种方法是使用 .cloneNode()方法,并在每次按下空格键时播放克隆的音频.

In order to play multiple instances of the sound at the same time, you will need to create a new copy of that Audio element. One way to do this is to use the .cloneNode() method and play the cloned audio every time the spacebar is pressed.

this.Sounds["L_Explosion1"].cloneNode(true).play();

这篇关于HTML5 JS同时播放多次相同的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆