(Web Audio API)振荡器节点错误:无法多次调用启动 [英] (Web Audio API) Oscillator node error: cannot call start more than once
问题描述
当我启动振荡器时,将其停止,然后重新启动;我收到以下错误:
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);
});
});
当前解决方案(在提问时): http://jsfiddle.net/xbqbzgt2/2/
Current solution (at time of question): http://jsfiddle.net/xbqbzgt2/2/
最终解决方案: http://jsfiddle.net/xbqbzgt2/3/
Final solution: http://jsfiddle.net/xbqbzgt2/3/
推荐答案
更好的方法是启动一次振荡器节点,并在需要时连接/断开振荡器节点,即:
A better way would be to start the oscillatorNode once and connect/disconnect the oscillatorNode from the graph when needed, ie :
var ctx = new AudioContext();
var osc = ctx.createOscillator();
osc.frequency.value = 8000;
osc.start();
$(document).ready(function() {
$("#start").click(function() {
osc.connect(ctx.destination);
});
$("#stop").click(function() {
osc.disconnect(ctx.destination);
});
});
这是如何在 mute the thermin (mozilla web audio api文档)
This how muting in done in muting the thermin (mozilla web audio api documentation)
这篇关于(Web Audio API)振荡器节点错误:无法多次调用启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!