[![npm][npm]][npm-url] [![deps][deps]][deps-url]
A speech-to-text library for React Native.
npm i react-native-microsoft-speech --save
Manually or automatically link the NativeModule
react-native link react-native-microsoft-speech
import Speech from 'react-native-microsoft-speech';
import React, {Component} from 'react';
class SpeechTest extends Component {
constructor(props) {
Speech.onSpeechStart = this.onSpeechStartHandler.bind(this);
Speech.onSpeechEnd = this.onSpeechEndHandler.bind(this);
Speech.onSpeechResults = this.onSpeechResultsHandler.bind(this);
}
onStartButtonPress(e){
Speech.start('en-US');
}
...
}
Static access to the Speech API.
All methods now return a new Promise
for async/await
compatibility.
Method Name | Description | Platform |
---|---|---|
Speech.isAvailable() | Checks whether a speech recognition service is available on the system. | Android, iOS |
Speech.start(locale) | Starts listening for speech for a specific locale. Returns null if no error occurs. | Android, iOS |
Speech.stop() | Stops listening for speech. Returns null if no error occurs. | Android, iOS |
Speech.cancel() | Cancels the speech recognition. Returns null if no error occurs. | Android, iOS |
Speech.destroy() | Destroys the current SpeechRecognizer instance. Returns null if no error occurs. | Android, iOS |
Speech.removeAllListeners() | Cleans/nullifies overridden Speech static methods. |
Android, iOS |
Speech.isRecognizing() | Return if the SpeechRecognizer is recognizing. | Android, iOS |
Callbacks that are invoked when a native event emitted.
Event Name | Description | Event | Platform |
---|---|---|---|
Speech.onSpeechStart(event) | Invoked when .start() is called without error. |
{ error: false } |
Android, iOS |
Speech.onSpeechRecognized(event) | Invoked when speech is recognized. | { error: false } |
Android, iOS |
Speech.onSpeechEnd(event) | Invoked when SpeechRecognizer stops recognition. | { error: false } |
Android, iOS |
Speech.onSpeechError(event) | Invoked when an error occurs. | { error: Description of error as string } |
Android, iOS |
Speech.onSpeechResults(event) | Invoked when SpeechRecognizer is finished recognizing. | { value: [..., 'Speech recognized'] } |
Android, iOS |
Speech.onSpeechPartialResults(event) | Invoked when any results are computed. | { value: [..., 'Partial speech recognized'] } |
Android, iOS |
Speech.onSpeechVolumeChanged(event) | Invoked when pitch that is recognized changed. | { value: pitch in dB } |
Android |
Arguably the most important part.
While the included VoiceTest
app works without explicit permissions checks and requests, it may be necessary to add a permission request for RECORD_AUDIO
for some configurations.
Since Android M (6.0), user need to grant permission at runtime (and not during app installation).
By default, calling the startSpeech
method will invoke RECORD AUDIO
permission popup to the user. This can be disabled by passing REQUEST_PERMISSIONS_AUTO: true
in the options argument.
Need to include permissions for NSMicrophoneUsageDescription
and NSSpeechRecognitionUsageDescription
inside Info.plist for iOS. See the included VoiceTest
for how to handle these cases.
<dict>
...
<key>NSMicrophoneUsageDescription</key>
<string>Description of why you require the use of the microphone</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>Description of why you require the use of the speech recognition</string>
...
</dict>
Please see the documentation provided by ReactNative for this: PermissionsAndroid