Skip to content

Instantly share code, notes, and snippets.

@aghyad97
aghyad97 / play-sound-web-api.ts
Created September 8, 2025 17:07
Playing sounds with Web APIs
const audioCtxRef = useRef<AudioContext | null>(null);
const ensureAudio = useCallback(() => {
if (audioCtxRef.current) return audioCtxRef.current;
const Ctx =
(window as any).AudioContext || (window as any).webkitAudioContext;
if (!Ctx) return null;
audioCtxRef.current = new Ctx();
return audioCtxRef.current;