feat(audio): improve resource cleanup for Safari compatibility

This commit is contained in:
Robert Dean 2025-08-04 09:48:55 -04:00
parent 81d9d7ca03
commit 608e9aa806

View file

@ -100,9 +100,22 @@ export function BinauralModal({ onClose, show }: BinauralProps) {
const stopSound = useCallback(() => { const stopSound = useCallback(() => {
if (!isPlaying) return; if (!isPlaying) return;
// Stop oscillators
leftOscillatorRef.current?.stop(); leftOscillatorRef.current?.stop();
rightOscillatorRef.current?.stop(); rightOscillatorRef.current?.stop();
audioContextRef.current?.close();
// Unload oscillators
leftOscillatorRef.current = null;
rightOscillatorRef.current = null;
// Close and unload AudioContext
if (audioContextRef.current) {
audioContextRef.current.close();
audioContextRef.current = null;
}
// Unload gain node
gainNodeRef.current = null;
setIsPlaying(false); setIsPlaying(false);
}, [isPlaying]); }, [isPlaying]);
@ -127,11 +140,9 @@ export function BinauralModal({ onClose, show }: BinauralProps) {
useEffect(() => { useEffect(() => {
// Cleanup when component unmounts // Cleanup when component unmounts
return () => { return () => {
if (isPlaying) {
stopSound(); stopSound();
}
}; };
}, [isPlaying, stopSound]); }, [stopSound]);
useEffect(() => { useEffect(() => {
// Update frequencies when a preset is selected // Update frequencies when a preset is selected