How to start sound after sound in soundmanager2

How can I start sound after sound in soundmanager2, I mean when the first sound ends, the second sound starts and when the second end of the third start, etc. etc.

+7
source share
4 answers
var soundArray = ['sound1', 'sound2', ...]; var chain = function (sound) { soundManager.play(sound, { multiShotEvents: true, onfinish: function () { var index = soundArray.indexOf(sound); if (soundArray[index + 1] !== undefined) { chain(soundArray[index + 1]); } }}); }; chain(soundArray[0]) 

the tray uses recursion, the only problem can occur when you add the same sound twice in the array (the chain is infinite)

+6
source

Below is an example of how to do this in the documentation (see Demo 4a). The play method takes an object as an argument. This object contains a set of parameters. One of these options is the onfinish callback function:

 soundManager.play('firstSound',{ multiShotEvents: true, onfinish:function() { soundManager.play('secondSound'); } }); 

The multiShotEvents parameter must be set to true for the onfinish event onfinish fire at the end of each sound. By default, it will only work after the sounds are completed.

You could queue as many sounds as you really wanted.

+1
source

It took me 3 hours to understand ... but here it is:

 soundManager.play('sound1',{ multiShotEvents: true, onfinish:function() { soundManager.play('sound2',{ multiShotEvents: true, onfinish:function() { soundManager.play('sound3'); } }); } }); 
+1
source

In addition to Ashot's answer and all the rest, you can set the onfinish function when creating sound, not only during playback, for example:

 var demo2Sound = soundManager.createSound({ url: '../mpc/audio/CHINA_1.mp3', onfinish: function() { soundManager._writeDebug(this.id + ' finished playing'); } }); 

... and if you need to change the onfinish function after the sound is already playing, you can do it like this:

 demo2Sound._onfinish = function(){ //now this function will override the previous handler }; 
+1
source

All Articles