capacitor-microphone 0.0.7 → 0.0.8
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.
|
@@ -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,117 @@ 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
|
-
|
|
119
|
-
|
|
117
|
+
call.reject("Speech recognition not available on this device");
|
|
118
|
+
return;
|
|
120
119
|
}
|
|
121
120
|
|
|
122
|
-
|
|
121
|
+
if (speechRecognizer != null) {
|
|
122
|
+
call.reject("Speech recognition already running");
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
123
125
|
|
|
124
|
-
|
|
126
|
+
this.currentCall = call;
|
|
125
127
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
|
|
130
|
-
);
|
|
131
|
-
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "es-MX");
|
|
132
|
-
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
|
|
128
|
+
String lang = call.getString("lang", "es-MX");
|
|
129
|
+
|
|
130
|
+
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getActivity());
|
|
133
131
|
|
|
134
132
|
speechRecognizer.setRecognitionListener(new RecognitionListener() {
|
|
135
133
|
|
|
134
|
+
@Override public void onReadyForSpeech(Bundle params) {}
|
|
135
|
+
|
|
136
|
+
@Override public void onBeginningOfSpeech() {}
|
|
137
|
+
|
|
138
|
+
@Override public void onRmsChanged(float rmsdB) {}
|
|
139
|
+
|
|
140
|
+
@Override public void onBufferReceived(byte[] buffer) {}
|
|
141
|
+
|
|
142
|
+
@Override public void onEndOfSpeech() {}
|
|
143
|
+
|
|
136
144
|
@Override
|
|
137
|
-
public void
|
|
138
|
-
|
|
139
|
-
|
|
145
|
+
public void onError(int error) {
|
|
146
|
+
notifyListeners("partialResult", new JSObject()
|
|
147
|
+
.put("text", "")
|
|
148
|
+
.put("isFinal", true)
|
|
149
|
+
.put("error", mapError(error))
|
|
150
|
+
);
|
|
140
151
|
|
|
141
|
-
JSObject ret = new JSObject();
|
|
142
|
-
ret.put("text", matches != null && !matches.isEmpty() ? matches.get(0) : "");
|
|
143
|
-
ret.put("isFinal", true);
|
|
144
152
|
if (currentCall != null) {
|
|
145
|
-
currentCall.
|
|
153
|
+
currentCall.reject(mapError(error));
|
|
146
154
|
currentCall = null;
|
|
147
155
|
}
|
|
156
|
+
stopSpeech();
|
|
148
157
|
}
|
|
149
158
|
|
|
150
159
|
@Override
|
|
151
|
-
public void
|
|
160
|
+
public void onResults(Bundle results) {
|
|
152
161
|
ArrayList<String> matches =
|
|
153
|
-
|
|
162
|
+
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
|
|
163
|
+
|
|
164
|
+
if (matches != null && matches.size() > 0) {
|
|
165
|
+
String text = matches.get(0);
|
|
154
166
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
ret.put("isFinal", false);
|
|
167
|
+
notifyListeners("partialResult", new JSObject()
|
|
168
|
+
.put("text", text)
|
|
169
|
+
.put("isFinal", true));
|
|
159
170
|
|
|
160
|
-
|
|
171
|
+
if (currentCall != null) {
|
|
172
|
+
currentCall.resolve(new JSObject()
|
|
173
|
+
.put("text", text)
|
|
174
|
+
.put("isFinal", true));
|
|
175
|
+
currentCall = null;
|
|
176
|
+
}
|
|
161
177
|
}
|
|
178
|
+
stopSpeech();
|
|
162
179
|
}
|
|
163
180
|
|
|
164
|
-
@Override
|
|
165
|
-
|
|
166
|
-
|
|
181
|
+
@Override
|
|
182
|
+
public void onPartialResults(Bundle partialResults) {
|
|
183
|
+
ArrayList<String> matches =
|
|
184
|
+
partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
|
|
167
185
|
|
|
168
|
-
|
|
186
|
+
if (matches != null && matches.size() > 0) {
|
|
187
|
+
notifyListeners("partialResult", new JSObject()
|
|
188
|
+
.put("text", matches.get(0))
|
|
189
|
+
.put("isFinal", false));
|
|
169
190
|
}
|
|
170
191
|
}
|
|
171
192
|
|
|
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
193
|
@Override public void onEvent(int eventType, Bundle params) {}
|
|
179
194
|
});
|
|
180
195
|
|
|
181
|
-
|
|
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);
|
|
182
203
|
}
|
|
183
204
|
|
|
205
|
+
|
|
206
|
+
|
|
184
207
|
@PluginMethod
|
|
185
208
|
public void stopListening(PluginCall call) {
|
|
209
|
+
stopSpeech();
|
|
210
|
+
call.resolve(new JSObject().put("stopped", true));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private void stopSpeech() {
|
|
186
214
|
if (speechRecognizer != null) {
|
|
187
215
|
speechRecognizer.stopListening();
|
|
188
216
|
speechRecognizer.destroy();
|
|
189
217
|
speechRecognizer = null;
|
|
190
218
|
}
|
|
191
|
-
|
|
192
|
-
JSObject ret = new JSObject();
|
|
193
|
-
ret.put("stopped", true);
|
|
194
|
-
call.resolve(ret);
|
|
195
219
|
}
|
|
196
220
|
|
|
221
|
+
|
|
197
222
|
@Override
|
|
198
223
|
protected void handleOnDestroy() {
|
|
199
224
|
if (speechRecognizer != null) {
|