如何在 soundmanager2 中连续播放声音

发布于 2024-12-06 07:14:33 字数 79 浏览 0 评论 0原文

我如何在 soundmanager2 中开始一个又一个的声音,我的意思是当第一个声音结束时,第二个声音开始,第二个声音结束时第三个声音开始等等。

How can i start sound after sound in soundmanager2, i mean when the first sound ends, second sound to start and when the second end the third start and etc and etc..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

峩卟喜欢 2024-12-13 07:14:33
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])

托盘使用递归,唯一的问题可能发生,当在数组中放置相同的声音两次(链是无穷大)

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)

云柯 2024-12-13 07:14:33

文档中有一个如何执行此操作的示例(请参阅演示 4a )。 play 方法采用一个对象作为参数。该对象包含一组选项。这些选项之一是 onfinish 回调函数:

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

multiShotEvents 选项必须设置为 true 才能引发 onfinish > 每个声音完成后触发的事件。默认情况下,只有声音结束后才会触发。

您确实可以通过这种方式对任意数量的声音进行排队。

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.

窝囊感情。 2024-12-13 07:14:33

我花了大约 3 个小时才弄清楚..但它是:

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

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');
      }
    });
  }
});
紅太極 2024-12-13 07:14:33

除了 Ashot 的答案和所有其他答案之外,您还可以在创建声音时设置 onfinish 功能,而不仅仅是在播放声音时设置,例如:

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

...如果您需要在声音已经播放后更改 onfinish 功能,你可以这样做:

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

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 
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文