问题描述
我有一段代码看起来像下面,它的工作正常。它的作用是当鼠标点击一个名为synth的div时启动振荡器声音。然后当鼠标按钮释放时,它会停止播放振荡器。 synth.onmousedown = function(){
振荡器= context.createOscillator(),//创建振荡器
oscillator.type = synthWaveform.value;
oscillator.frequency.value = pitchInput.value;
osc.connect(context.destination); //将其连接到输出
oscillator.noteOn(0);
};
synth.onmouseup = function(){
oscillator.disconnect();
};
然后,我添加了以下代码,以便用户按下键盘字符时触发振荡器。问题是,当按住键时,它会弹出播放,然后当键盘字符被释放时不会停止。我想知道是否有任何图书馆或代码可以用来规避这个。目标是让最终的结果像一个mousedown / up效果,但键盘运动触发声音。谢谢。
$('body')。keydown(function(){
oscillator = context.createOscillator(),//创建振荡器
oscillator.type = synthWaveform.value;
osc.frequency。 value = pitchInput.value;
振荡器连接(context.destination); //将其连接到输出
oscillator.noteOn(0);
});
$('body')。keyup(function(){
oscillator.disconnect();
});
你可以使用underscore.js去抖动,如果你需要的东西优于下列选项。
var keydown = false;
$('body')。keydown(function(){
if(!keydown){
oscillator = context.createOscillator(),//创建振荡器
振荡器类型= synthWaveform.value;
振荡器frequency.value = pitchInput.value;
振荡器连接(context.destination); //将其连接到输出
osc.noteOn (0);
keydown = true;
}
});
$('body')。keyup(function(){
oscillator.disconnect();
keydown = false;
});
I have a piece of code that looks something like below and it works fine. What it does is it launches an oscillator sound when the mouse clicks on a div named synth.It then stops playing the oscillator when the mouse button is released.
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();
};
I then added the code below so that when a user presses a keyboard charachter it triggers the oscillator. The problem is that when the key is held down it repeadedly plays and then won't stop when the keyboard character is released. I want to know if there are any libraries or code that can be used to circumvent this. The goal is to have the end result be like a mousedown/up effect but with keyboard movements triggering the sound.Thank you.
$('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();
});
you could use underscore.js debounce if you need something fancier than the below option.
var keydown = false;
$('body').keydown(function() {
if(!keydown){
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);
keydown = true;
}
});
$('body').keyup(function() {
oscillator.disconnect();
keydown = false;
});
这篇关于当你想要使用javascript的时候你把钥匙扣下来,但是只想玩一次,你如何规避重复的keydown消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!