【问题标题】:Calling Tone.JS function doesn't work second time第二次调用 Tone.JS 函数不起作用
【发布时间】:2020-06-11 15:48:19
【问题描述】:

当我第一次按下按钮时,它会按预期播放音符列表,但不是第二次。我的控制台日志记录表明第二次以相同的方式调用该函数,但也许我遗漏了什么?为什么这不会播放第二次音符序列?

我尝试了 Tone.Transport.start(0) 等其他潜在解决方案,但这并没有解决问题。

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src= "www/js/Tone.js"></script>
    <script src= "main.js"></script>
</head>
<body>

    <button onclick="playSeq([60, 61, 62], false, this.id, 'tone');">Play</button>

</body>
</html>

Javascript

function playSeq (note_list, hidePlay, id, sound) {


  midi_list = note_list.map(x => Tone.Frequency(x, "midi").toNote());
  last_note = midi_list.length;
  count = 0;
  var pattern = new Tone.Sequence(function(time, note){

  triggerNote(sound, note, 0.25);
  count = count + 1;

  if (count === last_note) {
  console.log("finished!");
  }

  }, midi_list);

  pattern.start(0).loop = false;

  console.log(Tone.Transport);
  // begin at the beginning
  Tone.Transport.start();


}

【问题讨论】:

    标签: javascript tone.js


    【解决方案1】:

    我不得不在播放结束后对 Transport 和 Tone 序列对象调用 .stop() 来解决问题:

    if (count === last_note) {
          pattern.stop();
          Tone.Transport.stop();
        }
    

    【讨论】:

      猜你喜欢
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      • 2016-03-30
      相关资源
      最近更新 更多