"use client"; import { Volume2 } from "lucide-react"; import { Label } from "@/components/ui/label"; import { emotionOptions, r2UrlAudio, openaiVoices } from "@/lib/data"; import EmojiComponent from "../CreateCharacter/EmojiComponent"; import { useState } from "react"; import { Input } from "@/components/ui/input"; export const VoiceSettings = () => { const [audioElement, setAudioElement] = useState(null); const [previewingVoice, setPreviewingVoice] = useState(null); const previewVoice = (voiceId: OaiVoice) => { // If the same voice is clicked again while playing, pause it if (previewingVoice === voiceId && audioElement) { audioElement.pause(); audioElement.currentTime = 0; setPreviewingVoice(null); return; } // Stop any currently playing preview if (audioElement) { audioElement.pause(); audioElement.currentTime = 0; } const audioSampleUrl = `${r2UrlAudio}/${voiceId}.wav`; setPreviewingVoice(voiceId); // Create and play audio element const audio = new Audio(audioSampleUrl); setAudioElement(audio); // Play the audio audio.play().catch(error => { console.error("Error playing audio:", error); setPreviewingVoice(null); }); // Reset the previewing state when audio ends audio.onended = () => { setPreviewingVoice(null); }; // Fallback in case audio doesn't trigger onended setTimeout(() => { if (previewingVoice === voiceId) { setPreviewingVoice(null); } }, 10000); // 10 second fallback }; return (
{openaiVoices.map((voice) => (
{ previewVoice(voice.id as OaiVoice); }} >
{voice.name} {voice.description}
{previewingVoice === voice.id && (
)}
))}
{emotionOptions.map((emotion) => (
{emotion.label}
))}
); }