当我第一次按下按钮时,它会按预期播放音符列表,但第二次不播放。我的控制台日志记录表明,以相同的方式再次调用该函数,但是也许我遗漏了一些东西?为什么第二次不播放音符序列?
我尝试了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();
}
答案 0 :(得分:0)
完成播放后,我必须在Transport和Tone序列对象上调用.stop()来解决问题:
if (count === last_note) {
pattern.stop();
Tone.Transport.stop();
}