When I start my oscillator, stop it, and then start it again; I get the following error:
Uncaught InvalidStateError: Failed to execute 'start' on 'OscillatorNode': cannot call start more than once.
Obviously I could use gain
to "stop" the audio but that strikes me as poor practice. What's a more efficient way of stopping the oscillator while being able to start it again?
var ctx = new AudioContext();
var osc = ctx.createOscillator();
osc.frequency.value = 8000;
osc.connect(ctx.destination);
function startOsc(bool) {
if(bool === undefined) bool = true;
if(bool === true) {
osc.start(ctx.currentTime);
} else {
osc.stop(ctx.currentTime);
}
}
$(document).ready(function() {
$("#start").click(function() {
startOsc();
});
$("#stop").click(function() {
startOsc(false);
});
});
Current solution (at time of question): http://jsfiddle.net/xbqbzgt2/2/
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…