如何在 howlerjs 的声音结束时淡出?

How to make a fade-out at the end of the sound in howlerjs?

在howler.js中可以进行淡入淡出。

sound1.fade(1, 0, 2000);

但是当 sound1 开始播放时,这个淡入淡出会触发。

如何在 howler.js 中给定声音的最后 2 秒淡出?

我能想到的最好的方法就是等待适当的时间褪色:

sound1.on('play', function(){
  var fadeouttime = 2000;
  setTimeout(
    function(){
      sound1.fade(1, 0, fadeouttime);
    },
    (sound1.duration() - sound1.seek())*1000 - fadeouttime
  );
});

类似的东西。您唯一需要担心的是在音频暂停、停止等情况下取消超时。我不知道有更优雅的解决方案。