capacitor-microphone 0.0.10 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -35,6 +35,7 @@ public class CapacitorMicrophonePlugin extends Plugin {
|
|
|
35
35
|
private SpeechRecognizer speechRecognizer;
|
|
36
36
|
private PluginCall currentCall;
|
|
37
37
|
private Intent speechIntent;
|
|
38
|
+
private boolean isListening = false;
|
|
38
39
|
|
|
39
40
|
|
|
40
41
|
@PluginMethod
|
|
@@ -124,6 +125,7 @@ private Intent speechIntent;
|
|
|
124
125
|
}
|
|
125
126
|
|
|
126
127
|
this.currentCall = call;
|
|
128
|
+
isListening = true;
|
|
127
129
|
|
|
128
130
|
String lang = call.getString("lang", "es-MX");
|
|
129
131
|
|
|
@@ -141,38 +143,42 @@ private Intent speechIntent;
|
|
|
141
143
|
|
|
142
144
|
@Override
|
|
143
145
|
public void onError(int error) {
|
|
146
|
+
if (!isListening) return;
|
|
147
|
+
|
|
144
148
|
if (error == SpeechRecognizer.ERROR_NO_MATCH ||
|
|
145
149
|
error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
|
|
146
|
-
speechRecognizer.startListening(speechIntent);
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
150
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
151
|
+
if (isListening) {
|
|
152
|
+
speechRecognizer.startListening(speechIntent);
|
|
153
|
+
}
|
|
154
|
+
return;
|
|
153
155
|
}
|
|
154
156
|
|
|
155
157
|
stopSpeech();
|
|
156
158
|
}
|
|
157
159
|
|
|
158
160
|
|
|
161
|
+
|
|
159
162
|
@Override
|
|
160
163
|
public void onResults(Bundle results) {
|
|
164
|
+
if (!isListening) return;
|
|
165
|
+
|
|
161
166
|
ArrayList<String> matches =
|
|
162
167
|
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
|
|
163
168
|
|
|
164
169
|
if (matches != null && !matches.isEmpty()) {
|
|
165
|
-
String text = matches.get(0);
|
|
166
|
-
|
|
167
170
|
notifyListeners("partialResult", new JSObject()
|
|
168
|
-
.put("text",
|
|
171
|
+
.put("text", matches.get(0))
|
|
169
172
|
.put("isFinal", true));
|
|
170
173
|
}
|
|
171
174
|
|
|
172
|
-
|
|
175
|
+
if (isListening) {
|
|
176
|
+
speechRecognizer.startListening(speechIntent);
|
|
177
|
+
}
|
|
173
178
|
}
|
|
174
179
|
|
|
175
180
|
|
|
181
|
+
|
|
176
182
|
@Override
|
|
177
183
|
public void onPartialResults(Bundle partialResults) {
|
|
178
184
|
ArrayList<String> matches =
|
|
@@ -215,28 +221,32 @@ private Intent speechIntent;
|
|
|
215
221
|
|
|
216
222
|
@PluginMethod
|
|
217
223
|
public void stopListening(PluginCall call) {
|
|
224
|
+
isListening = false;
|
|
218
225
|
currentCall = null;
|
|
219
226
|
stopSpeech();
|
|
220
227
|
call.resolve();
|
|
221
228
|
}
|
|
222
229
|
|
|
230
|
+
|
|
223
231
|
private void stopSpeech() {
|
|
224
|
-
|
|
225
|
-
speechRecognizer
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
232
|
+
try {
|
|
233
|
+
if (speechRecognizer != null) {
|
|
234
|
+
speechRecognizer.cancel();
|
|
235
|
+
speechRecognizer.destroy();
|
|
236
|
+
speechRecognizer = null;
|
|
237
|
+
}
|
|
238
|
+
} catch (Exception ignored) {}
|
|
229
239
|
}
|
|
230
240
|
|
|
231
241
|
|
|
242
|
+
|
|
232
243
|
@Override
|
|
233
244
|
protected void handleOnDestroy() {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
speechRecognizer = null;
|
|
237
|
-
}
|
|
245
|
+
isListening = false;
|
|
246
|
+
stopSpeech();
|
|
238
247
|
}
|
|
239
248
|
|
|
249
|
+
|
|
240
250
|
private String mapError(int error) {
|
|
241
251
|
switch (error) {
|
|
242
252
|
case SpeechRecognizer.ERROR_AUDIO: return "Audio error";
|