capacitor-microphone 0.0.8 → 0.0.9

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.
@@ -114,7 +114,7 @@ private Intent speechIntent;
114
114
  }
115
115
 
116
116
  if (!SpeechRecognizer.isRecognitionAvailable(getContext())) {
117
- call.reject("Speech recognition not available on this device");
117
+ call.reject("Speech recognition not available");
118
118
  return;
119
119
  }
120
120
 
@@ -127,83 +127,90 @@ private Intent speechIntent;
127
127
 
128
128
  String lang = call.getString("lang", "es-MX");
129
129
 
130
- speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getActivity());
131
-
132
- speechRecognizer.setRecognitionListener(new RecognitionListener() {
133
-
134
- @Override public void onReadyForSpeech(Bundle params) {}
130
+ getActivity().runOnUiThread(() -> {
131
+ try {
132
+ speechRecognizer =
133
+ SpeechRecognizer.createSpeechRecognizer(getActivity());
134
+
135
+ speechRecognizer.setRecognitionListener(new RecognitionListener() {
136
+ @Override public void onReadyForSpeech(Bundle params) {}
137
+ @Override public void onBeginningOfSpeech() {}
138
+ @Override public void onRmsChanged(float rmsdB) {}
139
+ @Override public void onBufferReceived(byte[] buffer) {}
140
+ @Override public void onEndOfSpeech() {}
141
+
142
+ @Override
143
+ public void onError(int error) {
144
+ if (currentCall != null) {
145
+ currentCall.reject(mapError(error));
146
+ currentCall = null;
147
+ }
148
+ stopSpeech();
149
+ }
135
150
 
136
- @Override public void onBeginningOfSpeech() {}
151
+ @Override
152
+ public void onResults(Bundle results) {
153
+ ArrayList<String> matches =
154
+ results.getStringArrayList(
155
+ SpeechRecognizer.RESULTS_RECOGNITION);
156
+
157
+ if (matches != null && matches.size() > 0) {
158
+ String text = matches.get(0);
159
+
160
+ notifyListeners("partialResult", new JSObject()
161
+ .put("text", text)
162
+ .put("isFinal", true));
163
+
164
+ if (currentCall != null) {
165
+ currentCall.resolve(new JSObject()
166
+ .put("text", text)
167
+ .put("isFinal", true));
168
+ currentCall = null;
169
+ }
170
+ }
171
+ stopSpeech();
172
+ }
137
173
 
138
- @Override public void onRmsChanged(float rmsdB) {}
174
+ @Override
175
+ public void onPartialResults(Bundle partialResults) {
176
+ ArrayList<String> matches =
177
+ partialResults.getStringArrayList(
178
+ SpeechRecognizer.RESULTS_RECOGNITION);
179
+
180
+ if (matches != null && matches.size() > 0) {
181
+ notifyListeners("partialResult", new JSObject()
182
+ .put("text", matches.get(0))
183
+ .put("isFinal", false));
184
+ }
185
+ }
139
186
 
140
- @Override public void onBufferReceived(byte[] buffer) {}
187
+ @Override public void onEvent(int eventType, Bundle params) {}
188
+ });
141
189
 
142
- @Override public void onEndOfSpeech() {}
190
+ speechIntent = new Intent(
191
+ RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
192
+ speechIntent.putExtra(
193
+ RecognizerIntent.EXTRA_LANGUAGE_MODEL,
194
+ RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
195
+ speechIntent.putExtra(
196
+ RecognizerIntent.EXTRA_LANGUAGE, lang);
197
+ speechIntent.putExtra(
198
+ RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
143
199
 
144
- @Override
145
- public void onError(int error) {
146
- notifyListeners("partialResult", new JSObject()
147
- .put("text", "")
148
- .put("isFinal", true)
149
- .put("error", mapError(error))
150
- );
200
+ speechRecognizer.startListening(speechIntent);
151
201
 
202
+ } catch (Exception e) {
152
203
  if (currentCall != null) {
153
- currentCall.reject(mapError(error));
204
+ currentCall.reject(e.getMessage());
154
205
  currentCall = null;
155
206
  }
156
- stopSpeech();
157
- }
158
-
159
- @Override
160
- public void onResults(Bundle results) {
161
- ArrayList<String> matches =
162
- results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
163
-
164
- if (matches != null && matches.size() > 0) {
165
- String text = matches.get(0);
166
-
167
- notifyListeners("partialResult", new JSObject()
168
- .put("text", text)
169
- .put("isFinal", true));
170
-
171
- if (currentCall != null) {
172
- currentCall.resolve(new JSObject()
173
- .put("text", text)
174
- .put("isFinal", true));
175
- currentCall = null;
176
- }
177
- }
178
- stopSpeech();
179
- }
180
-
181
- @Override
182
- public void onPartialResults(Bundle partialResults) {
183
- ArrayList<String> matches =
184
- partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
185
-
186
- if (matches != null && matches.size() > 0) {
187
- notifyListeners("partialResult", new JSObject()
188
- .put("text", matches.get(0))
189
- .put("isFinal", false));
190
- }
191
207
  }
192
-
193
- @Override public void onEvent(int eventType, Bundle params) {}
194
208
  });
195
-
196
- speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
197
- speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
198
- RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
199
- speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, lang);
200
- speechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
201
-
202
- speechRecognizer.startListening(speechIntent);
203
209
  }
204
210
 
205
211
 
206
212
 
213
+
207
214
  @PluginMethod
208
215
  public void stopListening(PluginCall call) {
209
216
  stopSpeech();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-microphone",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "plugin to use the microphone",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",