如何在howler.js上链接声音 [英] How to chain sounds on howler.js
问题描述
我需要在howler.js中播放一些声音,我不知道如何链接它。
I need to play some sounds in howler.js the thing is that I don't know how to chain it.
例如,在字符串BCG
For example, at string BCG
需要播放b.ogg然后c.ogg和最后g.ogg
would need to play b.ogg then c.ogg and finally g.ogg
如果我只是使用(之后) loading):
If I just use (after loading):
sound.play('b');
sound.play('c');
sound.play('g');
所有这些都是开始和重叠,这不是我需要的。
All of them start and overlap which isn't what I need.
我看到有一个属性,但无法弄清楚如何正确使用它。
I see there's a onend property, however can't figure out how to use it properly.
问候。
推荐答案
您可以创建一个函数 playString(yourString)
,它将读取每个字符并动态设置声音的 onend
属性。以下示例应播放 BCGAC :
You could create a function playString(yourString)
that will read each character and dynamically set the onend
property of your sound. The following example should play B C G A C:
var sound = new Howl({
urls: ['http://shrt.tf/abcdefg.mp3'],
volume: 1,
sprite: {
a: [0, 600],
b: [700, 500],
c: [1200, 600],
d: [1900, 500],
e: [2400, 500],
f: [2900, 500],
g: [3400, 500],
}
});
Howl.prototype.playString = function(str){
if(str.length>1){
this._onend[0] = function(){this.playString(str.substring(1,str.length));};
} else {
this._onend[0] = function(){};
}
if(str.length>0){
this.play(str.substring(0,1));
}
};
sound.playString('bcgac');
<script src="http://shrt.tf/howler.js"></script>
请注意,当字符不在精灵中时,您也可以调整此函数,或者使用名称数组而不是字符串。
Note that you could also tweak this function to work when a character is not in the sprite, or to use an array of names instead of a string.
这篇关于如何在howler.js上链接声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!