capacitor-microphone 0.1.2 → 0.1.4
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.
|
@@ -45,6 +45,13 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
|
|
|
45
45
|
private final Handler handler = new Handler(Looper.getMainLooper());
|
|
46
46
|
private Runnable delayedStartRunnable;
|
|
47
47
|
|
|
48
|
+
private String lastPartial = "";
|
|
49
|
+
private long lastSpeechTime = 0;
|
|
50
|
+
private Runnable silenceWatcher;
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
48
55
|
@PluginMethod
|
|
49
56
|
public void checkPermission(PluginCall call) {
|
|
50
57
|
PermissionState mic = getPermissionState("microphone");
|
|
@@ -140,6 +147,8 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
|
|
|
140
147
|
|
|
141
148
|
isDestroyed = false;
|
|
142
149
|
isListening = true;
|
|
150
|
+
lastPartial = "";
|
|
151
|
+
|
|
143
152
|
|
|
144
153
|
if (getPermissionState("microphone") != PermissionState.GRANTED) {
|
|
145
154
|
call.reject("Microphone permission not granted");
|
|
@@ -165,19 +174,20 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
|
|
|
165
174
|
RecognizerIntent.EXTRA_CALLING_PACKAGE,
|
|
166
175
|
getActivity().getPackageName()
|
|
167
176
|
);
|
|
168
|
-
|
|
169
|
-
RecognizerIntent.EXTRA_PREFER_OFFLINE,
|
|
170
|
-
true
|
|
171
|
-
);
|
|
177
|
+
lastSpeechTime = System.currentTimeMillis();
|
|
172
178
|
speechRecognizer.startListening(recognizerIntent);
|
|
173
179
|
|
|
174
180
|
call.resolve();
|
|
181
|
+
startSilenceWatcher();
|
|
182
|
+
|
|
175
183
|
}
|
|
176
184
|
|
|
177
185
|
@PluginMethod
|
|
178
186
|
public void stopListening(PluginCall call) {
|
|
179
187
|
isListening = false;
|
|
180
188
|
isDestroyed = true;
|
|
189
|
+
silenceWatcher = null;
|
|
190
|
+
|
|
181
191
|
|
|
182
192
|
if (delayedStartRunnable != null) {
|
|
183
193
|
handler.removeCallbacks(delayedStartRunnable);
|
|
@@ -209,12 +219,21 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
|
|
|
209
219
|
lastStopTime = System.currentTimeMillis();
|
|
210
220
|
|
|
211
221
|
if (speechRecognizer != null) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
222
|
+
try {
|
|
223
|
+
speechRecognizer.stopListening();
|
|
224
|
+
speechRecognizer.cancel();
|
|
225
|
+
} catch (Exception ignored) {}
|
|
226
|
+
|
|
227
|
+
handler.postDelayed(() -> {
|
|
228
|
+
try {
|
|
229
|
+
speechRecognizer.destroy();
|
|
230
|
+
} catch (Exception ignored) {}
|
|
231
|
+
speechRecognizer = null;
|
|
232
|
+
}, 150);
|
|
215
233
|
}
|
|
216
234
|
}
|
|
217
235
|
|
|
236
|
+
|
|
218
237
|
private void restartRecognition() {
|
|
219
238
|
if (!isListening || isDestroyed) return;
|
|
220
239
|
|
|
@@ -226,6 +245,7 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
|
|
|
226
245
|
if (isListening && !isDestroyed && recognizerIntent != null) {
|
|
227
246
|
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getActivity());
|
|
228
247
|
speechRecognizer.setRecognitionListener(this);
|
|
248
|
+
lastSpeechTime = System.currentTimeMillis();
|
|
229
249
|
speechRecognizer.startListening(recognizerIntent);
|
|
230
250
|
}
|
|
231
251
|
}, 200);
|
|
@@ -239,11 +259,12 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
|
|
|
239
259
|
handleResults(results, false);
|
|
240
260
|
}
|
|
241
261
|
|
|
242
|
-
private void handleResults(
|
|
262
|
+
private void handleResults(Bundle results, boolean isFinal) {
|
|
243
263
|
if (!isListening || isDestroyed) return;
|
|
244
|
-
|
|
245
264
|
if (results == null) return;
|
|
246
265
|
|
|
266
|
+
lastSpeechTime = System.currentTimeMillis();
|
|
267
|
+
|
|
247
268
|
ArrayList<String> matches =
|
|
248
269
|
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
|
|
249
270
|
|
|
@@ -251,6 +272,8 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
|
|
|
251
272
|
|
|
252
273
|
String text = matches.get(0);
|
|
253
274
|
|
|
275
|
+
lastPartial = text;
|
|
276
|
+
|
|
254
277
|
JSObject data = new JSObject();
|
|
255
278
|
data.put("text", text);
|
|
256
279
|
data.put("isFinal", isFinal);
|
|
@@ -258,29 +281,56 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
|
|
|
258
281
|
notifyListeners("partialResult", data);
|
|
259
282
|
|
|
260
283
|
if (isFinal) {
|
|
284
|
+
lastPartial = "";
|
|
261
285
|
handler.postDelayed(this::restartRecognition, 100);
|
|
262
286
|
}
|
|
263
287
|
}
|
|
264
288
|
|
|
289
|
+
private void startSilenceWatcher() {
|
|
290
|
+
|
|
291
|
+
if (silenceWatcher != null) return;
|
|
292
|
+
|
|
293
|
+
silenceWatcher = new Runnable() {
|
|
294
|
+
@Override
|
|
295
|
+
public void run() {
|
|
296
|
+
|
|
297
|
+
if (!isListening || isDestroyed) {
|
|
298
|
+
silenceWatcher = null;
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
long diff = System.currentTimeMillis() - lastSpeechTime;
|
|
303
|
+
|
|
304
|
+
if (diff > 2500) {
|
|
305
|
+
restartRecognition();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
handler.postDelayed(this, 1000);
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
handler.postDelayed(silenceWatcher, 1000);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
265
318
|
@Override public void onError(int error) {
|
|
266
319
|
|
|
267
320
|
if (!isListening || isDestroyed) return;
|
|
268
321
|
|
|
269
|
-
if (
|
|
270
|
-
|
|
271
|
-
|
|
322
|
+
if (!lastPartial.isEmpty()) {
|
|
323
|
+
JSObject data = new JSObject();
|
|
324
|
+
data.put("text", lastPartial);
|
|
325
|
+
data.put("isFinal", true);
|
|
326
|
+
notifyListeners("partialResult", data);
|
|
327
|
+
lastPartial = "";
|
|
272
328
|
}
|
|
273
329
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT) {
|
|
330
|
+
handler.postDelayed(this::restartRecognition, 200);
|
|
331
|
+
}
|
|
277
332
|
|
|
278
|
-
handler.postDelayed(this::restartRecognition, 100);
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
333
|
|
|
282
|
-
stopSpeech();
|
|
283
|
-
}
|
|
284
334
|
|
|
285
335
|
@Override
|
|
286
336
|
protected void handleOnDestroy() {
|
|
@@ -294,7 +344,12 @@ public class CapacitorMicrophonePlugin extends Plugin implements RecognitionList
|
|
|
294
344
|
@Override public void onBeginningOfSpeech() {}
|
|
295
345
|
@Override public void onRmsChanged(float rmsdB) {}
|
|
296
346
|
@Override public void onBufferReceived(byte[] buffer) {}
|
|
297
|
-
@Override
|
|
347
|
+
@Override
|
|
348
|
+
public void onEndOfSpeech() {
|
|
349
|
+
if (!isListening || isDestroyed) return;
|
|
350
|
+
|
|
351
|
+
handler.postDelayed(this::restartRecognition, 100);
|
|
352
|
+
}
|
|
298
353
|
@Override public void onEvent(int eventType, @Nullable android.os.Bundle params) {}
|
|
299
354
|
|
|
300
355
|
}
|