【问题标题】:AudioContext: Oscillator - know when all sounds have been playedAudioContext: Oscillator - 知道所有声音何时播放完毕
【发布时间】:2017-09-01 14:55:39
【问题描述】:

我正在使用this javascript api (miniMusic)。我能够创建音乐,然后导出 javascript 代码。我也可以运行它。

我希望能够知道我的音乐何时结束,以便我可以再次播放并控制它。

with(new AudioContext)
for(i in D=[12,,,13,,,18,,,,,,12,,,13,,,18,,,,,,12,,,13,,,18,,,15,,,12,,,8,,,12,,,13]) {
   with(createOscillator())
   if(D[i]) {
      connect(destination) 
      frequency.value=800*1.06**(13-D[i]),
      type='square',
      start(i*.1),
      stop(i*.1+.1)
   }
}
// -> onEnd = function (...) {}

循环立即运行,因此我无法使用索引来定位音乐正在播放的音符。 有人可以帮我吗?

【问题讨论】:

    标签: javascript audiocontext


    【解决方案1】:

    Oscillator 有一个onend 函数,当音调结束时调用它,但是您链接的 api 为每个音符创建一个新的振荡器,您可以计算播放的音符数量,然后在音符数量等于时循环乐曲中的音符数。

    例子

    with(new AudioContext)
    for (i in D = [12, , , 13, , , 18, , , , , , 12, , , 13, , , 18, , , , , , 12, , , 13, , , 18, , , 15, , , 12, , , 8, , , 12, , , 13]) {
      with(createOscillator())
      if (D[i]) {
        onended = function() {
          console.log('Note has stopped playing');
        }
        connect(destination)
        frequency.value = 800 * 1.06 ** (13 - D[i]),
          type = 'square',
          start(i * .1),
          stop(i * .1 + .1)
      }
    }

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多