buzzSound.ts - Defer AudioContext creation

This commit is contained in:
2023-07-16 21:58:15 +05:30
parent fdf70d5977
commit 455f0cdda5
+4 -1
View File
@@ -1,6 +1,6 @@
// The browser will limit the number of concurrent audio contexts // The browser will limit the number of concurrent audio contexts
// So be sure to re-use them whenever you can // So be sure to re-use them whenever you can
const myAudioContext = new AudioContext(); let myAudioContext: AudioContext | undefined = undefined;
let lastOscillatorNode: OscillatorNode | undefined = undefined; let lastOscillatorNode: OscillatorNode | undefined = undefined;
/** /**
@@ -18,6 +18,9 @@ export function startBuzz({
volume = 70, volume = 70,
onEnd = () => {}, onEnd = () => {},
} = {}) { } = {}) {
if (myAudioContext === undefined) {
myAudioContext = new AudioContext();
}
if (lastOscillatorNode !== undefined) { if (lastOscillatorNode !== undefined) {
lastOscillatorNode.stop(myAudioContext.currentTime + duration * 0.001); lastOscillatorNode.stop(myAudioContext.currentTime + duration * 0.001);
return { return {