capacitor-microphone 0.0.7 → 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.
@@ -1,5 +1,5 @@
1
1
  <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
2
 
3
3
  <uses-permission android:name="android.permission.RECORD_AUDIO" />
4
-
4
+ <uses-permission android:name="android.permission.INTERNET" />
5
5
  </manifest>
@@ -17,6 +17,8 @@ import android.speech.RecognitionListener;
17
17
  import android.speech.RecognizerIntent;
18
18
  import android.speech.SpeechRecognizer;
19
19
 
20
+
21
+
20
22
  import java.util.ArrayList;
21
23
 
22
24
  @CapacitorPlugin(
@@ -32,6 +34,8 @@ public class CapacitorMicrophonePlugin extends Plugin {
32
34
 
33
35
  private SpeechRecognizer speechRecognizer;
34
36
  private PluginCall currentCall;
37
+ private Intent speechIntent;
38
+
35
39
 
36
40
  @PluginMethod
37
41
  public void checkPermission(PluginCall call) {
@@ -104,96 +108,124 @@ private PluginCall currentCall;
104
108
  @PluginMethod
105
109
  public void startListening(PluginCall call) {
106
110
 
107
- if (speechRecognizer != null) {
108
- call.reject("Speech recognition already running");
109
- return;
110
- }
111
-
112
111
  if (getPermissionState("microphone") != PermissionState.GRANTED) {
113
112
  call.reject("Microphone permission not granted");
114
113
  return;
115
114
  }
116
115
 
117
116
  if (!SpeechRecognizer.isRecognitionAvailable(getContext())) {
118
- call.reject("Speech recognition not available on this device");
119
- return;
117
+ call.reject("Speech recognition not available");
118
+ return;
120
119
  }
121
120
 
122
- currentCall = call;
123
-
124
- speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getContext());
125
-
126
- Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
127
- intent.putExtra(
128
- RecognizerIntent.EXTRA_LANGUAGE_MODEL,
129
- RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
130
- );
131
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "es-MX");
132
- intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
133
-
134
- speechRecognizer.setRecognitionListener(new RecognitionListener() {
135
-
136
- @Override
137
- public void onResults(Bundle results) {
138
- ArrayList<String> matches =
139
- results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
121
+ if (speechRecognizer != null) {
122
+ call.reject("Speech recognition already running");
123
+ return;
124
+ }
140
125
 
141
- JSObject ret = new JSObject();
142
- ret.put("text", matches != null && !matches.isEmpty() ? matches.get(0) : "");
143
- ret.put("isFinal", true);
126
+ this.currentCall = call;
127
+
128
+ String lang = call.getString("lang", "es-MX");
129
+
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
+ }
150
+
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
+ }
173
+
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
+ }
186
+
187
+ @Override public void onEvent(int eventType, Bundle params) {}
188
+ });
189
+
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);
199
+
200
+ speechRecognizer.startListening(speechIntent);
201
+
202
+ } catch (Exception e) {
144
203
  if (currentCall != null) {
145
- currentCall.resolve(ret);
204
+ currentCall.reject(e.getMessage());
146
205
  currentCall = null;
147
206
  }
148
207
  }
208
+ });
209
+ }
149
210
 
150
- @Override
151
- public void onPartialResults(Bundle partialResults) {
152
- ArrayList<String> matches =
153
- partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
154
-
155
- if (matches != null && !matches.isEmpty()) {
156
- JSObject ret = new JSObject();
157
- ret.put("text", matches.get(0));
158
- ret.put("isFinal", false);
159
-
160
- notifyListeners("partialResult", ret);
161
- }
162
- }
163
-
164
- @Override public void onError(int error) {
165
- if (currentCall != null) {
166
- currentCall.reject(mapError(error));
167
-
168
- currentCall = null;
169
- }
170
- }
171
211
 
172
- // Métodos obligatorios (vacíos)
173
- @Override public void onReadyForSpeech(Bundle params) {}
174
- @Override public void onBeginningOfSpeech() {}
175
- @Override public void onRmsChanged(float rmsdB) {}
176
- @Override public void onBufferReceived(byte[] buffer) {}
177
- @Override public void onEndOfSpeech() {}
178
- @Override public void onEvent(int eventType, Bundle params) {}
179
- });
180
212
 
181
- speechRecognizer.startListening(intent);
182
- }
183
213
 
184
214
  @PluginMethod
185
215
  public void stopListening(PluginCall call) {
216
+ stopSpeech();
217
+ call.resolve(new JSObject().put("stopped", true));
218
+ }
219
+
220
+ private void stopSpeech() {
186
221
  if (speechRecognizer != null) {
187
222
  speechRecognizer.stopListening();
188
223
  speechRecognizer.destroy();
189
224
  speechRecognizer = null;
190
225
  }
191
-
192
- JSObject ret = new JSObject();
193
- ret.put("stopped", true);
194
- call.resolve(ret);
195
226
  }
196
227
 
228
+
197
229
  @Override
198
230
  protected void handleOnDestroy() {
199
231
  if (speechRecognizer != null) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-microphone",
3
- "version": "0.0.7",
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",