capacitor-microphone 0.0.13 → 0.0.14

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.
@@ -37,6 +37,9 @@ private PluginCall currentCall;
37
37
  private Intent speechIntent;
38
38
  private boolean isListening = false;
39
39
  private boolean isDestroyed = false;
40
+ private long lastStopTime = 0;
41
+ private static final long RESTART_COOLDOWN_MS = 400;
42
+
40
43
 
41
44
 
42
45
  @PluginMethod
@@ -110,6 +113,25 @@ private boolean isDestroyed = false;
110
113
  @PluginMethod
111
114
  public void startListening(PluginCall call) {
112
115
 
116
+
117
+ long now = System.currentTimeMillis();
118
+ long diff = now - lastStopTime;
119
+
120
+ if (diff < RESTART_COOLDOWN_MS) {
121
+ long delay = RESTART_COOLDOWN_MS - diff;
122
+
123
+ getActivity().runOnUiThread(() -> {
124
+ getActivity()
125
+ .getWindow()
126
+ .getDecorView()
127
+ .postDelayed(() -> startListening(call), delay);
128
+ });
129
+ return;
130
+ }
131
+
132
+
133
+ isDestroyed = false;
134
+ isListening = true;
113
135
 
114
136
 
115
137
  if (getPermissionState("microphone") != PermissionState.GRANTED) {
@@ -122,22 +144,23 @@ private boolean isDestroyed = false;
122
144
  return;
123
145
  }
124
146
 
147
+
125
148
  if (speechRecognizer != null) {
126
149
  stopSpeech();
127
150
  }
128
151
 
129
152
  this.currentCall = call;
130
- isListening = true;
131
- isDestroyed = false;
132
153
 
133
154
  String lang = call.getString("lang", "es-MX");
134
155
 
156
+
135
157
  getActivity().runOnUiThread(() -> {
136
158
  try {
137
159
  speechRecognizer =
138
160
  SpeechRecognizer.createSpeechRecognizer(getActivity());
139
161
 
140
162
  speechRecognizer.setRecognitionListener(new RecognitionListener() {
163
+
141
164
  @Override public void onReadyForSpeech(Bundle params) {}
142
165
  @Override public void onBeginningOfSpeech() {}
143
166
  @Override public void onRmsChanged(float rmsdB) {}
@@ -160,20 +183,21 @@ private boolean isDestroyed = false;
160
183
  stopSpeech();
161
184
  }
162
185
 
163
-
164
-
165
-
166
186
  @Override
167
187
  public void onResults(Bundle results) {
168
188
  if (!isListening) return;
169
189
 
170
190
  ArrayList<String> matches =
171
- results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
191
+ results.getStringArrayList(
192
+ SpeechRecognizer.RESULTS_RECOGNITION);
172
193
 
173
194
  if (matches != null && !matches.isEmpty()) {
174
- notifyListeners("partialResult", new JSObject()
175
- .put("text", matches.get(0))
176
- .put("isFinal", true));
195
+ notifyListeners(
196
+ "partialResult",
197
+ new JSObject()
198
+ .put("text", matches.get(0))
199
+ .put("isFinal", true)
200
+ );
177
201
  }
178
202
 
179
203
  if (!isDestroyed && isListening && speechRecognizer != null) {
@@ -181,24 +205,28 @@ private boolean isDestroyed = false;
181
205
  }
182
206
  }
183
207
 
184
-
185
-
186
208
  @Override
187
209
  public void onPartialResults(Bundle partialResults) {
210
+ if (!isListening || isDestroyed) return;
211
+
188
212
  ArrayList<String> matches =
189
213
  partialResults.getStringArrayList(
190
214
  SpeechRecognizer.RESULTS_RECOGNITION);
191
215
 
192
- if (matches != null && matches.size() > 0) {
193
- notifyListeners("partialResult", new JSObject()
194
- .put("text", matches.get(0))
195
- .put("isFinal", false));
216
+ if (matches != null && !matches.isEmpty()) {
217
+ notifyListeners(
218
+ "partialResult",
219
+ new JSObject()
220
+ .put("text", matches.get(0))
221
+ .put("isFinal", false)
222
+ );
196
223
  }
197
224
  }
198
225
 
199
226
  @Override public void onEvent(int eventType, Bundle params) {}
200
227
  });
201
228
 
229
+
202
230
  speechIntent = new Intent(
203
231
  RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
204
232
  speechIntent.putExtra(
@@ -209,6 +237,7 @@ private boolean isDestroyed = false;
209
237
  speechIntent.putExtra(
210
238
  RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
211
239
 
240
+
212
241
  speechRecognizer.startListening(speechIntent);
213
242
 
214
243
  } catch (Exception e) {
@@ -221,8 +250,11 @@ private boolean isDestroyed = false;
221
250
  }
222
251
 
223
252
 
253
+
224
254
  private void stopSpeech() {
225
255
  try {
256
+ lastStopTime = System.currentTimeMillis();
257
+
226
258
  if (speechRecognizer != null) {
227
259
  speechRecognizer.cancel();
228
260
  speechRecognizer.setRecognitionListener(null);
@@ -234,6 +266,7 @@ private boolean isDestroyed = false;
234
266
 
235
267
 
236
268
 
269
+
237
270
  @PluginMethod
238
271
  public void stopListening(PluginCall call) {
239
272
  isListening = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-microphone",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "description": "plugin to use the microphone",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",