react-speech-recognizer-component

1.0.7 • Public • Published

react-speech-recognizer

React Component for Speech Recognition.

This HOC component allows to get Speech Recognition capabilities into your app. By wrapping your component on it, you can have it changing depending on the transcripts you get back or the SpeechRecognizer.status.

It relies on the Web Speech Recognition API and if it's not supported in the browser, it logs a warn, renders nothing and calls the onError prop (if provided).

It creates a SpeechRecognition instance internally and exposes its events through props in the component.

Use it like:

import React from 'react';
import logo from './logo.svg';
import './App.css';

import { SpeechRecognizer } from 'react-speech-recognizer-component';

function App() {
  return (
    <div className="App">
      <h1>Speech to text test with React Speech Recognizer</h1>
      <SpeechRecognizer
        startSpeechRecognition={true}
        onError={this.onError}
      >
          {({status, results, formattedResults, transcripts, error}) => {
            return (
              <>
                {transcripts && transcripts.length && <p>{transcripts.join(', ')}</p>}
              </>
            );
          }}
      </SpeechRecognizer>
    </div>
  );
}

export default App;

props

Same as the ones that the SpeechRecognition object would accept, plus the one that says start recognizing, yo!.

  • startSpeechRecognition: flag that if true the recoznizer starts attempting to listen on your microphone.
  • grammars: Accepts a string in the (beautiful!) JSGF Format.
  • continuous: Flag that if true more than one result per recognition is returned.
  • interimResults: Flag that if true returns results that are not final.
  • maxAlternatives: Yup, the most alternatives that can be returned. Keep in mind that setting continous to false (which is the default value)
  • lang: ISO string with the language.

(Some) of the SpeechRecognition events are also exposed as props.

  • onStart: Callback run on the onstart event of the SpeechRecognizer instance.
  • onError: Callback run on the onerror event of the SpeechRecognizer instance. One of the cases when this is executed when SpeechRecognition is not supported by the browser.

Next steps

  1. Probably start over with a different idea in mind, based on what I learn while working on Guess The Movie (repo here). It'd be a VoiceCommandRecognizer instead. :)

Package Sidebar

Install

npm i react-speech-recognizer-component

Weekly Downloads

7

Version

1.0.7

License

MIT

Unpacked Size

14.2 kB

Total Files

4

Last publish

Collaborators

  • loqtor