Skip to content

Latest commit

Β 

History

History
54 lines (43 loc) Β· 2.4 KB

useSpeechRecognition.md

File metadata and controls

54 lines (43 loc) Β· 2.4 KB

πŸ– useSpeechRecognition

Uses the browser's SpeechRecognition API

Arguments

  • onEnd: Function: Called when SpeechRecognition stops listening.
  • onResult: Function(string): Called when SpeechRecognition has a result. It is called with a string containing a transcript of the recognized speech.
  • onError: Function(event): Called when SpeechRecognition has an error

Returns

Usage

import { useSpeechRecognition } from 'react-recipes';

function App() {
  const [value, setValue] = useState('');
  const [ended, setEnded] = useState(false);
  const onResult = (result) => setValue(result.join(''));
  const onEnd = () => setEnded(true);
  const { listen, listening, stop, supported } = useSpeechRecognition({ onEnd, onResult });

  if (!supported) {
    return 'Speech Recognition is not supported. Upgrade your browser';
  }

  const onListen = () => {
    listen({ lang: 'en-US', continuous: true });
  };

  return (
    <div>
      <textarea value={value} onChange={(event) => setValue(event.target.value)} />
      <button onMouseDown={onListen} onMouseUp={stop} type="button">
        🎀
      </button>
      {listening && <div>Go ahead I'm listening</div>}
      <p>{ended && 'Speech has stoped listening'}</p>
    </div>
  );
}