问题描述
我'编写一个Javascript的比赛,我想在多次使用一个声音。我可以加载一个声音多次到数组做到这一点。但我想加载一个声音一次,它复制到数组,这样我就可以多次播放。这是什么样的方法我现在有:
I'am programming a Javascript game and I want to use one sound in multiple times. I can do it with loading one sound more times to an array. But I want to load one sound once and "copy" it to array, so I can play it multiple times. This is method what I have now:
this.list = [
"LaserShot", //http://www.freesound.org/samplesViewSingle.php?id=39459
"Ding", //http://www.freesound.org/samplesViewSingle.php?id=5212
"Rocket" //http://www.freesound.org/samplesViewSingle.php?id=47252 -> http://creativecommons.org/licenses/sampling+/1.0/
];
...
for (i in this.list) {
this.sounds[this.list[i]] = new Array();
for (var j = 0; j < this.channels; j++) {
this.sounds[this.list[i]][j] = new Audio("./sounds/"+ this.list[i] + type);
}
}
我只是想做到这一点:
I just want to do this:
for (i in this.list) {
this.sounds[this.list[i]] = new Array();
var tempAudio = new Audio("./sounds/"+ this.list[i] + type);
for (var j = 0; j < this.channels; j++) {
this.sounds[this.list[i]][j] = realCopyOfTempAudio;
}
}
感谢您这么多。
推荐答案
我的经验:
最好是在同一个源创造更多的音频html标签。我是JS的粉丝,但是这一次却是最好有HTML音频标签在HTML表单。
我做了一个重复的音频标签和我饰我的需求。如果你想在一秒钟内播放同样的声音几次,再添加更多的克隆。
I made a duplicate audio tags and I adorned my needs . if you want to play the same sound several times in one second , then add more clones.
也可以修复自动播放的bug
(而不是EXE_JUST_ONE_TIME可以使用覆盖单击事件,并不重要了):
also you can fix the autoplay bug( instead of EXE_JUST_ONE_TIME you can use override click event , not important now ) :
<audio controls id="LaserShot" >
<source src="LaserShot.mp3" type="audio/mpeg">
<source src="LaserShot.ogg" type="audio/ogg">
</audio>
<audio controls id="LaserShot_CLONE" >
<source src="LaserShot.mp3" type="audio/mpeg">
<source src="LaserShot.ogg" type="audio/ogg">
</audio>
VAR EXE_JUST_ONE_TIME = FALSE;
var EXE_JUST_ONE_TIME = false;
document.addEventListener(点击,功能(E){
document.addEventListener("click" , function(e) {
if (EXE_JUST_ONE_TIME == false){
EXE_JUST_ONE_TIME = true;
document.getElementById("LaserShot").play();
document.getElementById("LaserShot").pause();
// now you can play programmability from js
document.getElementById("LaserShot_CLONE").play();
document.getElementById("LaserShot_CLONE").pause();
}
}
最后一部分(需要处理),此处理程序仅适用于一个克隆:
Last part (need to be handled) this handler works only for one clone:
var play_SHOT = function(){
if (document.getElementById('LaserShot').duration > 0 && !document.getElementById('LaserShot').paused) {
document.getElementById('LaserShot_CLONE').play();
}
else {
document.getElementById('LaserShot').play();
}
}
这篇关于HTML5的音频播放多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!