簡體   English   中英

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

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

當我第一次按下按鈕時,它會按預期播放筆記列表,但第二次不會。 我的控制台日志記錄表明第二次以相同的方式調用 function,但也許我遺漏了什么? 為什么這不會第二次播放音符序列?

我在其他可能的解決方案中嘗試了例如 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();


}

回放結束后,我不得不在 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