-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.android.js
65 lines (60 loc) · 1.71 KB
/
index.android.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {
SpeechRecognizer,
RecognizerIntent,
RecognitionListener
} from 'react-native-android-speech-recognizer';
export const isSpeechRecognitionSupported = () =>
!!SpeechRecognizer;
export class SpeechRecognition {
_getOrCreateSpeechRecognizer() {
if (this._recognizer) {
return Promise.resolve(this._recognizer);
}
return SpeechRecognizer.createSpeechRecognizer().then(recognizer => {
this._recognizer = recognizer;
return recognizer;
});
}
abort() {
if (this._recognizer) {
this._recognizer.destroy();
}
}
start() {
this._getOrCreateSpeechRecognizer().then(recognizer => {
const listeners = {};
if (this.onspeechstart) {
listeners.onBeginningOfSpeech = () => this.onspeechstart();
}
if (this.onspeechend) {
listeners.onEndOfSpeech = () => this.onspeechend();
}
if (this.onerror) {
listeners.onError = event => this.onerror({
error: "Failed with error code: " + event.error
});
}
listeners.onResults = event => {
const recognition = event.results[SpeechRecognizer.RESULTS_RECOGNITION];
const bestRecognition = recognition[0];
const speechRecognitionEvent = {
resultIndex: 0,
results: [[{
transcript: bestRecognition
}]],
}
speechRecognitionEvent.results[0].isFinal = true;
if (this.onresult) {
this.onresult(speechRecognitionEvent);
}
}
recognizer.setRecognitionListener(listeners);
recognizer.startListening(RecognizerIntent.ACTION_RECOGNIZE_SPEECH, {});
});
}
stop() {
if (this._recognizer) {
this._recognizer.cancel();
}
}
}