简体   繁体   English

第二次调用 Tone.JS function 不起作用

[英]Calling Tone.JS function doesn't work second time

When I press the button the first time, it plays back the list of notes as expected, but not the second time.当我第一次按下按钮时,它会按预期播放笔记列表,但第二次不会。 My console logging suggests that the second time around the function is called in the same way, but perhaps I am missing something?我的控制台日志记录表明第二次以相同的方式调用 function,但也许我遗漏了什么? Why will this not play the sequence of notes the second time?为什么这不会第二次播放音符序列?

I tried eg Tone.Transport.start(0) among other potential solutions but this didn't solve the problem.我在其他可能的解决方案中尝试了例如 Tone.Transport.start(0) 但这并没有解决问题。

HTML 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 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();


}

I had to call.stop() on the Transport and the Tone sequence objects after the finished playing back to solve the problem:回放结束后,我不得不在 Transport 和 Tone 序列对象上调用 .stop() 来解决问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM