screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script type="text/babel"> const { useState, useEffect } = React; function App() { const [text, setText] = useState('This is Font Awesome Icons Site'); const [speaking, setSpeaking] = useState(false); const [voices, setVoices] = useState([]); const [selectedVoice, setSelectedVoice] = useState(null); useEffect(() => { function populateVoices() { const voiceList = window.speechSynthesis.getVoices(); setVoices(voiceList); setSelectedVoice(voiceList.find(voice => voice.default)); } populateVoices(); // Call initially // Listen for voiceschanged event to update the list of voices window.speechSynthesis.addEventListener('voiceschanged', populateVoices); }, []); const handleTextChange = (event) => { setText(event.target.value); }; const handleVoiceChange = (event) => { const selectedVoice = voices[event.target.value]; setSelectedVoice(selectedVoice); }; const handleSpeak = () => { if (text && !speaking) { const utterance = new SpeechSynthesisUtterance(text); if (selectedVoice) { utterance.voice = selectedVoice; } utterance.addEventListener('start', () => { setSpeaking(true); }); utterance.addEventListener('end', () => { setSpeaking(false); }); window.speechSynthesis.speak(utterance); } }; const handleStop = () => { if (speaking) { window.speechSynthesis.cancel(); setSpeaking(false); } }; return ( <div className='container'> <h3>React Js Convert Text to Speech</h3> <textarea value={text} onChange={handleTextChange} placeholder="Enter text to convert to speech" /> <select onChange={handleVoiceChange} value={voices.indexOf(selectedVoice)}> <option value={-1}>Default Voice</option> {voices.map((voice, index) => ( <option key={index} value={index}> {voice.name} </option> ))} </select> <button onClick={handleSpeak} disabled={speaking}> Speak </button> <button onClick={handleStop} disabled={!speaking}> Stop </button> </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style> /* Styling for the container */ .container { max-width: 800px; margin: 0 auto; padding: 20px; font-family: Arial, sans-serif; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); } textarea { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 5px; margin-bottom: 10px; font-size: 14px; resize: vertical; } select { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 5px; margin-bottom: 10px; font-size: 14px; } button { padding: 8px 15px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease; font-size: 14px; } button:disabled { background-color: #ccc; cursor: not-allowed; } button:hover { background-color: #0056b3; } button+button { margin-left: 10px; } </style> </body> </html>