开发者

How to start sound after sound in soundmanager2

How can i start sound after sound in soundmanager2, i mean when th开发者_如何学运维e first sound ends, second sound to start and when the second end the third start and etc and etc..


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])

tray use recursion, the only problem can occur, when in array you put same sound twice(chain be infinity)


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

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

The multiShotEvents option has to be set to true to cause the onfinish event to fire upon completion of each sound. By default it will only fire once sounds have finished.

You could queue up as many sounds as you wanted to this way really.


It took me like 3 hours to figure out.. but here it is:

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


In addition to Ashot's answer and all the others, you can set the onfinish function when you create the sound, not only when you play it, 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 once the sound is already playing, you can do so like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜