我有一段代码,看起来像下面,它工作正常。 它所做的是它启动的振荡器的声音时,在一个div名为synth.It点击鼠标即可停止播放振荡器鼠标按钮被释放时。
synth.onmousedown= function () {
oscillator = context.createOscillator(), // Creates the oscillator
oscillator.type = synthWaveform.value;
oscillator.frequency.value = pitchInput.value;
oscillator.connect(context.destination); // Connects it to output
oscillator.noteOn(0);
};
synth.onmouseup = function () {
oscillator.disconnect();
};
然后我添加下面的代码,这样,当用户按下键盘charachter它触发振荡器。 问题是,键盘字符被释放时,当键被按下它repeadedly剧本,然后就不会停止。 我想知道是否有可以用来规避这个任何库或代码。 我们的目标是让最终的结果是像一个鼠标按下/时生效,但与键盘的动作触发sound.Thank你。
$('body').keydown(function() {
oscillator = context.createOscillator(), // Creates the oscillator
oscillator.type = synthWaveform.value;
oscillator.frequency.value = pitchInput.value;
oscillator.connect(context.destination); // Connects it to output
oscillator.noteOn(0);
});
$('body').keyup(function() {
oscillator.disconnect();
});