import React, { useState, useEffect, useRef } from 'react' interface BluePlayButtonProps { text?: string lang?: string disabled?: boolean className?: string size?: 'sm' | 'md' | 'lg' title?: string rate?: number pitch?: number volume?: number onPlayStart?: () => void onPlayEnd?: () => void onError?: (error: string) => void } export const BluePlayButton: React.FC = ({ text, lang = 'en-US', disabled = false, className = '', size = 'md', title, rate = 0.9, pitch = 1.0, volume = 1.0, onPlayStart, onPlayEnd, onError }) => { const [isPlaying, setIsPlaying] = useState(false) const [isSupported, setIsSupported] = useState(true) const utteranceRef = useRef(null) const sizeClasses = { sm: 'w-8 h-8', md: 'w-10 h-10', lg: 'w-12 h-12' } const iconSizes = { sm: 'w-4 h-4', md: 'w-5 h-5', lg: 'w-6 h-6' } // 檢查瀏覽器支援 useEffect(() => { if (typeof window !== 'undefined') { setIsSupported('speechSynthesis' in window) } }, []) // 清理未完成的語音 useEffect(() => { return () => { if (utteranceRef.current) { speechSynthesis.cancel() } } }, []) // 停止當前播放 const stopSpeech = () => { if (utteranceRef.current) { speechSynthesis.cancel() utteranceRef.current = null } setIsPlaying(false) if (onPlayEnd) onPlayEnd() } // 開始 TTS 播放 const startSpeech = (textToSpeak: string) => { try { // 停止任何正在進行的語音 speechSynthesis.cancel() const utterance = new SpeechSynthesisUtterance(textToSpeak) utteranceRef.current = utterance // 設置語音參數 utterance.lang = lang utterance.rate = Math.max(0.1, Math.min(2.0, rate)) // 限制範圍 0.1-2.0 utterance.pitch = Math.max(0, Math.min(2, pitch)) // 限制範圍 0-2 utterance.volume = Math.max(0, Math.min(1, volume)) // 限制範圍 0-1 // 事件處理 utterance.onstart = () => { setIsPlaying(true) if (onPlayStart) onPlayStart() } utterance.onend = () => { utteranceRef.current = null setIsPlaying(false) if (onPlayEnd) onPlayEnd() } utterance.onerror = (event) => { utteranceRef.current = null setIsPlaying(false) const errorMessage = `Speech synthesis error: ${event.error}` console.warn(errorMessage) if (onError) onError(errorMessage) if (onPlayEnd) onPlayEnd() } // 開始播放 speechSynthesis.speak(utterance) } catch (error) { const errorMessage = `Failed to start speech synthesis: ${error}` console.error(errorMessage) setIsPlaying(false) if (onError) onError(errorMessage) } } // 主處理函數 const handleToggle = () => { // 如果不支援 TTS if (!isSupported) { const errorMessage = 'Text-to-speech is not supported in this browser' console.warn(errorMessage) if (onError) onError(errorMessage) return } // 停止播放邏輯 if (isPlaying) { stopSpeech() return } // 開始播放邏輯 if (onPlayStart && !text) { // 自定義播放場景(如錄音回放) setIsPlaying(true) onPlayStart() // 3秒後自動停止(可調整) setTimeout(() => { setIsPlaying(false) if (onPlayEnd) onPlayEnd() }, 3000) } else if (text) { // 標準 TTS 播放 startSpeech(text) } else { const errorMessage = 'No text provided for speech synthesis' console.warn(errorMessage) if (onError) onError(errorMessage) } } // 計算按鈕狀態 const isDisabled = disabled || !isSupported const buttonTitle = title || (!isSupported ? "此瀏覽器不支援語音播放" : isPlaying ? "點擊停止播放" : text ? "點擊播放發音" : "點擊播放") return ( ) }