import React from 'react'; import { IconButton, Tooltip, CircularProgress, Box, Menu, MenuItem, ListItemIcon, ListItemText, FormControl, Select, Slider, Typography } from '@mui/material'; import { VolumeUp as VolumeUpIcon, Stop as StopIcon, PlayArrow as PlayArrowIcon, Settings as SettingsIcon } from '@mui/icons-material'; import { useTextToSpeech, SpeechSynthesisOptions } from '../../hooks/useTextToSpeech'; interface TextToSpeechButtonProps { text: string; textToSpeak?: string; // Optional different text to speak (e.g., shorter version) options?: SpeechSynthesisOptions; size?: 'small' | 'medium' | 'large'; showSettings?: boolean; disabled?: boolean; } export const TextToSpeechButton: React.FC = ({ text, textToSpeak, options, size = 'medium', showSettings = false, disabled = false, }) => { const { speak, stop, isSpeaking, isSupported, voices, pause, resume, isPaused } = useTextToSpeech(); const [anchorEl, setAnchorEl] = React.useState(null); const [selectedVoice, setSelectedVoice] = React.useState(null); const [rate, setRate] = React.useState(1); const [pitch, setPitch] = React.useState(1); const [volume, setVolume] = React.useState(1); const handleClick = (event: React.MouseEvent) => { if (showSettings) { setAnchorEl(event.currentTarget); } }; const handleClose = () => { setAnchorEl(null); }; const handleSpeak = () => { const textToUse = textToSpeak || text; if (!textToUse.trim()) return; if (isSpeaking) { stop(); } else { speak(textToUse, { voice: selectedVoice || undefined, rate, pitch, volume, ...options, }); } }; if (!isSupported) { return null; } const iconSize = size === 'small' ? 18 : size === 'medium' ? 24 : 30; const buttonSize = size === 'small' ? 'small' : size === 'medium' ? 'medium' : 'large'; return ( {isSpeaking ? : } {showSettings && ( <> Voice Settings {/* Voice Selection */} Voice {/* Speed */} Speed: {rate}x setRate(value as number)} size="small" sx={{ mb: 2 }} /> {/* Pitch */} Pitch: {pitch} setPitch(value as number)} size="small" sx={{ mb: 2 }} /> {/* Volume */} Volume: {Math.round(volume * 100)}% setVolume(value as number)} size="small" /> )} ); }; export default TextToSpeechButton;