我为我演奏的每一个音符制作一个新的振荡器。
function playSound(freq, duration) {
var attack = 5,
decay = duration,
gain = context.createGain(),
osc = context.createOscillator();
gain.connect(context.destination);
gain.gain.setValueAtTime(0, context.currentTime);
gain.gain.linearRampToValueAtTime(0.1, context.currentTime + attack / 1000);
gain.gain.linearRampToValueAtTime(0, context.currentTime + decay / 1000);
osc.frequency.value = freq;
osc.type = "sine";
osc.connect(gain);
osc.start(0);
setTimeout(function() {
osc.stop(0);
osc.disconnect(gain);
gain.disconnect(context.destination);
}, decay)
}旋律在一个for循环中播放,其中调用了playSound。当我单击pause按钮时,我想使旋律静音,并暂停for循环,以便再次单击play按钮时,旋律继续播放。如何访问所有电流振荡器以断开它们?
发布于 2014-04-11 23:58:51
在此代码中,您不能这样做。
1)根据设计,在Web Audio API中没有节点图的自省-它支持优化垃圾收集,并针对大量节点进行优化。两种可能的解决方案-要么维护播放振荡器的列表,要么将它们全部连接到单个增益节点(即,将它们的包络增益节点连接到“混合器”增益节点),然后断开连接(并释放对该增益节点的引用)。
2)不确定您所说的“暂停for循环”是什么意思--我假设您在play note方法周围有一个for循环?
发布于 2021-06-05 04:39:08
您可以suspend音频上下文。
const audioCtx = new AudioContext();
audioCtx.suspend();
audioCtx.resume();https://stackoverflow.com/questions/23016365
复制相似问题