From 455f0cdda52f23a09b4302a45b39808f452fa9a8 Mon Sep 17 00:00:00 2001 From: bendtherules Date: Sun, 16 Jul 2023 21:58:15 +0530 Subject: [PATCH] buzzSound.ts - Defer AudioContext creation --- src/helpers/buzzSound.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/helpers/buzzSound.ts b/src/helpers/buzzSound.ts index dd82c49..8e3ca29 100644 --- a/src/helpers/buzzSound.ts +++ b/src/helpers/buzzSound.ts @@ -1,6 +1,6 @@ // The browser will limit the number of concurrent audio contexts // So be sure to re-use them whenever you can -const myAudioContext = new AudioContext(); +let myAudioContext: AudioContext | undefined = undefined; let lastOscillatorNode: OscillatorNode | undefined = undefined; /** @@ -18,6 +18,9 @@ export function startBuzz({ volume = 70, onEnd = () => {}, } = {}) { + if (myAudioContext === undefined) { + myAudioContext = new AudioContext(); + } if (lastOscillatorNode !== undefined) { lastOscillatorNode.stop(myAudioContext.currentTime + duration * 0.001); return {