'use client'; import { useState } from 'react'; export interface AudioPlayerProps { text: string; lang?: string; onPlayStart?: () => void; onPlayEnd?: () => void; onError?: (error: string) => void; className?: string; disabled?: boolean; } export default function AudioPlayer({ text, lang = 'en-US', onPlayStart, onPlayEnd, onError, className = '', disabled = false }: AudioPlayerProps) { const [isPlaying, setIsPlaying] = useState(false); // TTS播放控制功能 const toggleTTS = () => { if (!('speechSynthesis' in window)) { onError?.('您的瀏覽器不支援語音播放'); return; } // 如果正在播放,則停止 if (isPlaying) { speechSynthesis.cancel(); setIsPlaying(false); onPlayEnd?.(); return; } // 開始播放 speechSynthesis.cancel(); setIsPlaying(true); onPlayStart?.(); const utterance = new SpeechSynthesisUtterance(text); utterance.lang = lang; utterance.rate = 0.8; // 稍慢語速 utterance.pitch = 1.0; utterance.volume = 1.0; utterance.onend = () => { setIsPlaying(false); onPlayEnd?.(); }; utterance.onerror = () => { setIsPlaying(false); onError?.('語音播放失敗'); }; speechSynthesis.speak(utterance); }; return ( ); }