forked from react-native-voice/voice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (155 loc) · 3.87 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';
import React, {
NativeModules,
NativeEventEmitter,
Platform,
} from 'react-native';
const { Voice } = NativeModules;
const voiceEmitter = new NativeEventEmitter(Voice);
class RCTVoice {
constructor() {
this._loaded = false;
this._listeners = null;
this._events = {
'onSpeechStart': this._onSpeechStart.bind(this),
'onSpeechRecognized': this._onSpeechRecognized.bind(this),
'onSpeechEnd': this._onSpeechEnd.bind(this),
'onSpeechError': this._onSpeechError.bind(this),
'onSpeechResults': this._onSpeechResults.bind(this),
'onSpeechPartialResults': this._onSpeechPartialResults.bind(this),
'onSpeechVolumeChanged': this._onSpeechVolumeChanged.bind(this)
};
}
removeAllListeners() {
Voice.onSpeechStart = null;
Voice.onSpeechRecognized = null;
Voice.onSpeechEnd = null;
Voice.onSpeechError = null;
Voice.onSpeechResults = null;
Voice.onSpeechPartialResults = null;
Voice.onSpeechVolumeChanged = null;
}
destroy() {
if (!this._loaded && !this._listeners) {
return;
}
return new Promise((resolve, reject) => {
Voice.destroySpeech((error) => {
if (error) {
reject(new Error(error));
} else {
if (this._listeners) {
this._listeners.map((listener, index) => listener.remove());
this._listeners = null;
}
resolve();
}
});
});
}
start(locale, options = {}) {
if (!this._loaded && !this._listeners) {
this._listeners = Object.keys(this._events)
.map((key, index) => voiceEmitter.addListener(key, this._events[key]));
}
return new Promise((resolve, reject) => {
const callback = (error) => {
if (error) {
reject(new Error(error));
} else {
resolve();
}
};
if (Platform.OS === 'android') {
Voice.startSpeech(locale, Object.assign({
EXTRA_LANGUAGE_MODEL: "LANGUAGE_MODEL_FREE_FORM",
EXTRA_MAX_RESULTS: 5,
EXTRA_PARTIAL_RESULTS: true,
REQUEST_PERMISSIONS_AUTO: true,
}, options), callback);
} else {
Voice.startSpeech(locale, callback);
}
});
}
stop() {
if (!this._loaded && !this._listeners) {
return;
}
return new Promise((resolve, reject) => {
Voice.stopSpeech((error) => {
if (error) {
reject(new Error(error));
} else {
resolve();
}
});
});
}
cancel() {
if (!this._loaded && !this._listeners) {
return;
}
return new Promise((resolve, reject) => {
Voice.cancelSpeech((error) => {
if (error) {
reject(new Error(error));
} else {
resolve();
}
});
});
}
isAvailable() {
return new Promise((resolve, reject) => {
Voice.isSpeechAvailable((isAvailable, error) => {
if (error) {
reject(new Error(error));
} else {
resolve(isAvailable);
}
});
});
}
isRecognizing() {
return new Promise((resolve, reject) => {
Voice.isRecognizing(isRecognizing => resolve(isRecognizing));
});
}
_onSpeechStart(e) {
if (this.onSpeechStart) {
this.onSpeechStart(e);
}
}
_onSpeechRecognized(e) {
if (this.onSpeechRecognized) {
this.onSpeechRecognized(e);
}
}
_onSpeechEnd(e) {
if (this.onSpeechEnd) {
this.onSpeechEnd(e);
}
}
_onSpeechError(e) {
if (this.onSpeechError) {
this.onSpeechError(e);
}
}
_onSpeechResults(e) {
if (this.onSpeechResults) {
this.onSpeechResults(e);
}
}
_onSpeechPartialResults(e) {
if (this.onSpeechPartialResults) {
this.onSpeechPartialResults(e);
}
}
_onSpeechVolumeChanged(e) {
if (this.onSpeechVolumeChanged) {
this.onSpeechVolumeChanged(e);
}
}
}
export default new RCTVoice();