118 | Emit for handling raw events from
119 | SpeechRecognition
122 |
123 |
124 |
125 |
126 |
127 | ## Hooks
128 |
129 | > Although previous versions exported a React Context, it is recommended to use the hooks interface.
130 |
131 | | Name | Signature | Description |
132 | | --------------- | ----------- | --------------------------------------------------------------------------------------------------- |
133 | | `useAbortable` | `[boolean]` | If ongoing speech recognition has `abort()` function and can be aborted, `true`, otherwise, `false` |
134 | | `useReadyState` | `[number]` | Returns the current state of recognition, refer to [this section](#function-as-a-child) |
135 | | `useSupported` | `[boolean]` | If speech recognition is supported, `true`, otherwise, `false` |
136 |
137 | ### Checks if speech recognition is supported
138 |
139 | To determines whether speech recognition is supported in the browser:
140 |
141 | - If `speechRecognition` prop is `undefined`
142 | - If both [`window.navigator.mediaDevices`](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices) and [`window.navigator.mediaDevices.getUserMedia`](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia) are falsy, it is not supported
143 | - Probably the browser is not on a secure HTTP connection
144 | - If both `window.SpeechRecognition` and vendor-prefixed are falsy, it is not supported
145 | - If recognition failed once with `not-allowed` error code, it is not supported
146 | - Otherwise, it is supported
147 |
148 | > Even the browser is on an insecure HTTP connection, `window.SpeechRecognition` (or vendor-prefixed) will continue to be truthy. Instead, `mediaDevices.getUserMedia` is used for capability detection.
149 |
150 | ### Event lifecycle
151 |
152 | One of the design aspect is to make sure events are easy to understand and deterministic. First rule of thumb is to make sure `onProgress` will lead to either `onDictate` or `onError`. Here are some samples of event firing sequence (tested on Chrome 67):
153 |
154 | - Happy path: speech is recognized
155 | 1. `onStart`
156 | 1. `onProgress({})` (just started, therefore, no `results`)
157 | 1. `onProgress({ results: [] })`
158 | 1. `onDictate({ result: ... })`
159 | 1. `onEnd`
160 | - Happy path: speech is recognized with continuous mode
161 | 1. `onStart`
162 | 1. `onProgress({})` (just started, therefore, no `results`)
163 | 1. `onProgress({ results: [] })`
164 | 1. `onDictate({ result: ... })`
165 | 1. `onProgress({ results: [] })`
166 | 1. `onDictate({ result: ... })`
167 | 1. `onEnd`
168 | - Heard some sound, but nothing can be recognized
169 | 1. `onStart`
170 | 1. `onProgress({})`
171 | 1. `onDictate({})` (nothing is recognized, therefore, no `result`)
172 | 1. `onEnd`
173 | - Nothing is heard (audio device available but muted)
174 | 1. `onStart`
175 | 1. `onProgress({})`
176 | 1. `onError({ error: 'no-speech' })`
177 | 1. `onEnd`
178 | - Recognition aborted
179 | 1. `onStart`
180 | 1. `onProgress({})`
181 | 1. `onProgress({ results: [] })`
182 | 1. While speech is getting recognized, set `props.disabled` to `false`, abort recognition
183 | 1. `onError({ error: 'aborted' })`
184 | 1. `onEnd`
185 | - Not authorized to use speech or no audio device is availablabortable: truee
186 | 1. `onStart`
187 | 1. `onError({ error: 'not-allowed' })`
188 | 1. `onEnd`
189 |
190 | ## Function as a child
191 |
192 | Instead of passing child elements, you can pass a function to render different content based on ready state. This is called [function as a child](https://reactjs.org/docs/render-props.html#using-props-other-than-render).
193 |
194 | | Ready state | Description |
195 | | ----------- | -------------------------------------------------------------------------- |
196 | | `0` | Not started |
197 | | `1` | Starting recognition engine, recognition is not ready until it turn to `2` |
198 | | `2` | Recognizing |
199 | | `3` | Stopping |
200 |
201 | For example,
202 |
203 | ```jsx
204 |
205 | {({ readyState }) =>
206 | readyState === 0 ? 'Start' : readyState === 1 ? 'Starting...' : readyState === 2 ? 'Listening...' : 'Stopping...'
207 | }
208 |
209 | ```
210 |
211 | # Customization thru morphing
212 |
213 | You can build your own component by copying our layout code, without messing around the [logic code behind the scene](packages/component/src/Composer.js). For details, please refer to [`DictateButton.js`](packages/component/src/DictateButton.js), [`DictateCheckbox.js`](packages/component/src/DictateCheckbox.js), and [`DictationTextBox.js`](packages/pages/src/DictationTextBox.js).
214 |
215 | ## Checkbox version
216 |
217 | In addition to `