diff --git a/frontend/src/components/VoicePreviewButton.tsx b/frontend/src/components/VoicePreviewButton.tsx index 21aa6b0..7399154 100644 --- a/frontend/src/components/VoicePreviewButton.tsx +++ b/frontend/src/components/VoicePreviewButton.tsx @@ -1,4 +1,4 @@ -import { useState, useRef } from 'react'; +import { useState, useRef, useEffect } from 'react'; import { api } from '../lib/api'; interface VoicePreviewButtonProps { @@ -14,6 +14,22 @@ export function VoicePreviewButton({ voiceName, language, disabled }: VoicePrevi const audioRef = useRef(null); const audioUrlRef = useRef(null); + // Clear cached audio when voice or language changes + useEffect(() => { + // Stop any playing audio + if (audioRef.current) { + audioRef.current.pause(); + audioRef.current = null; + } + // Revoke old URL + if (audioUrlRef.current) { + URL.revokeObjectURL(audioUrlRef.current); + audioUrlRef.current = null; + } + setIsPlaying(false); + setError(null); + }, [voiceName, language]); + const handlePreview = async () => { setError(null);