|
| 1 | +package com.wenkesj.voice; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.speech.RecognitionListener; |
| 5 | +import android.speech.RecognizerIntent; |
| 6 | +import android.speech.SpeechRecognizer; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.os.Handler; |
| 9 | + |
| 10 | +import com.facebook.react.modules.core.DeviceEventManagerModule; |
| 11 | +import com.facebook.react.bridge.WritableMap; |
| 12 | +import com.facebook.react.bridge.WritableArray; |
| 13 | +import com.facebook.react.bridge.Arguments; |
| 14 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 15 | +import com.facebook.react.bridge.ReactContextBaseJavaModule; |
| 16 | +import com.facebook.react.bridge.ReactMethod; |
| 17 | +import com.facebook.react.bridge.Callback; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.Map; |
| 23 | +import java.lang.Exception; |
| 24 | +import java.lang.Runnable; |
| 25 | + |
| 26 | +import javax.annotation.Nullable; |
| 27 | + |
| 28 | +public class VoiceModule extends ReactContextBaseJavaModule implements RecognitionListener { |
| 29 | + |
| 30 | + final ReactApplicationContext reactContext; |
| 31 | + private SpeechRecognizer speech = null; |
| 32 | + private boolean isRecognizing = false; |
| 33 | + |
| 34 | + public VoiceModule(ReactApplicationContext reactContext) { |
| 35 | + super(reactContext); |
| 36 | + this.reactContext = reactContext; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public String getName() { |
| 41 | + return "RCTVoice"; |
| 42 | + } |
| 43 | + |
| 44 | + @ReactMethod |
| 45 | + public void startSpeech(final String locale, final Callback callback) { |
| 46 | + final VoiceModule self = this; |
| 47 | + Handler mainHandler = new Handler(this.reactContext.getMainLooper()); |
| 48 | + mainHandler.post(new Runnable() { |
| 49 | + |
| 50 | + private String getLocale(String locale){ |
| 51 | + if(locale != null && !locale.equals("")){ |
| 52 | + return locale; |
| 53 | + } |
| 54 | + |
| 55 | + return Locale.getDefault().toString(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void run() { |
| 60 | + try { |
| 61 | + speech = SpeechRecognizer.createSpeechRecognizer(self.reactContext); |
| 62 | + } catch(Exception e) { |
| 63 | + callback.invoke(e); |
| 64 | + } |
| 65 | + |
| 66 | + speech.setRecognitionListener(self); |
| 67 | + |
| 68 | + final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
| 69 | + intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); |
| 70 | + intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, getLocale(locale)); |
| 71 | + intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5); |
| 72 | + |
| 73 | + speech.startListening(intent); |
| 74 | + isRecognizing = true; |
| 75 | + callback.invoke(false); |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + @ReactMethod |
| 81 | + public void stopSpeech(final Callback callback) { |
| 82 | + Handler mainHandler = new Handler(this.reactContext.getMainLooper()); |
| 83 | + mainHandler.post(new Runnable() { |
| 84 | + @Override |
| 85 | + public void run() { |
| 86 | + try { |
| 87 | + speech.stopListening(); |
| 88 | + callback.invoke(false); |
| 89 | + } catch(Exception e) { |
| 90 | + callback.invoke(e); |
| 91 | + } |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + @ReactMethod |
| 97 | + public void cancelSpeech(final Callback callback) { |
| 98 | + Handler mainHandler = new Handler(this.reactContext.getMainLooper()); |
| 99 | + mainHandler.post(new Runnable() { |
| 100 | + @Override |
| 101 | + public void run() { |
| 102 | + try { |
| 103 | + speech.cancel(); |
| 104 | + isRecognizing = false; |
| 105 | + callback.invoke(false); |
| 106 | + } catch(Exception e) { |
| 107 | + callback.invoke(e); |
| 108 | + } |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + @ReactMethod |
| 114 | + public void destroySpeech(final Callback callback) { |
| 115 | + Handler mainHandler = new Handler(this.reactContext.getMainLooper()); |
| 116 | + mainHandler.post(new Runnable() { |
| 117 | + @Override |
| 118 | + public void run() { |
| 119 | + try { |
| 120 | + speech.destroy(); |
| 121 | + isRecognizing = false; |
| 122 | + callback.invoke(false); |
| 123 | + } catch(Exception e) { |
| 124 | + callback.invoke(e); |
| 125 | + } |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + @ReactMethod |
| 131 | + public void isSpeechAvailable(final Callback callback) { |
| 132 | + final VoiceModule self = this; |
| 133 | + Handler mainHandler = new Handler(this.reactContext.getMainLooper()); |
| 134 | + mainHandler.post(new Runnable() { |
| 135 | + @Override |
| 136 | + public void run() { |
| 137 | + try { |
| 138 | + Boolean isSpeechAvailable = SpeechRecognizer.isRecognitionAvailable(self.reactContext); |
| 139 | + callback.invoke(isSpeechAvailable, false); |
| 140 | + } catch(Exception e) { |
| 141 | + callback.invoke(false, e); |
| 142 | + } |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + @ReactMethod |
| 148 | + public void isRecognizing(Callback callback) { |
| 149 | + final VoiceModule self = this; |
| 150 | + callback.invoke(isRecognizing); |
| 151 | + } |
| 152 | + |
| 153 | + private void sendEvent(String eventName, @Nullable WritableMap params) { |
| 154 | + this.reactContext |
| 155 | + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) |
| 156 | + .emit(eventName, params); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void onBeginningOfSpeech() { |
| 161 | + WritableMap event = Arguments.createMap(); |
| 162 | + event.putBoolean("error", false); |
| 163 | + sendEvent("onSpeechStart", event); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void onBufferReceived(byte[] buffer) { |
| 168 | + WritableMap event = Arguments.createMap(); |
| 169 | + event.putBoolean("error", false); |
| 170 | + sendEvent("onSpeechRecognized", event); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void onEndOfSpeech() { |
| 175 | + WritableMap event = Arguments.createMap(); |
| 176 | + event.putBoolean("error", false); |
| 177 | + sendEvent("onSpeechEnd", event); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void onError(int errorCode) { |
| 182 | + String errorMessage = getErrorText(errorCode); |
| 183 | + WritableMap event = Arguments.createMap(); |
| 184 | + event.putString("error", errorMessage); |
| 185 | + sendEvent("onSpeechError", event); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void onEvent(int arg0, Bundle arg1) { } |
| 190 | + |
| 191 | + @Override |
| 192 | + public void onPartialResults(Bundle results) { |
| 193 | + WritableArray arr = Arguments.createArray(); |
| 194 | + |
| 195 | + ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); |
| 196 | + for (String result : matches) { |
| 197 | + arr.pushString(result); |
| 198 | + } |
| 199 | + |
| 200 | + WritableMap event = Arguments.createMap(); |
| 201 | + event.putArray("value", arr); |
| 202 | + sendEvent("onSpeechPartialResults", event); |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public void onReadyForSpeech(Bundle arg0) { |
| 207 | + WritableMap event = Arguments.createMap(); |
| 208 | + event.putBoolean("error", false); |
| 209 | + sendEvent("onSpeechStart", event); |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public void onResults(Bundle results) { |
| 214 | + WritableArray arr = Arguments.createArray(); |
| 215 | + |
| 216 | + ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); |
| 217 | + for (String result : matches) { |
| 218 | + arr.pushString(result); |
| 219 | + } |
| 220 | + |
| 221 | + WritableMap event = Arguments.createMap(); |
| 222 | + event.putArray("value", arr); |
| 223 | + sendEvent("onSpeechResults", event); |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public void onRmsChanged(float rmsdB) { |
| 228 | + WritableMap event = Arguments.createMap(); |
| 229 | + event.putDouble("value", (double) rmsdB); |
| 230 | + sendEvent("onSpeechVolumeChanged", event); |
| 231 | + } |
| 232 | + |
| 233 | + public static String getErrorText(int errorCode) { |
| 234 | + String message; |
| 235 | + switch (errorCode) { |
| 236 | + case SpeechRecognizer.ERROR_AUDIO: |
| 237 | + message = "Audio recording error"; |
| 238 | + break; |
| 239 | + case SpeechRecognizer.ERROR_CLIENT: |
| 240 | + message = "Client side error"; |
| 241 | + break; |
| 242 | + case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: |
| 243 | + message = "Insufficient permissions"; |
| 244 | + break; |
| 245 | + case SpeechRecognizer.ERROR_NETWORK: |
| 246 | + message = "Network error"; |
| 247 | + break; |
| 248 | + case SpeechRecognizer.ERROR_NETWORK_TIMEOUT: |
| 249 | + message = "Network timeout"; |
| 250 | + break; |
| 251 | + case SpeechRecognizer.ERROR_NO_MATCH: |
| 252 | + message = "No match"; |
| 253 | + break; |
| 254 | + case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: |
| 255 | + message = "RecognitionService busy"; |
| 256 | + break; |
| 257 | + case SpeechRecognizer.ERROR_SERVER: |
| 258 | + message = "error from server"; |
| 259 | + break; |
| 260 | + case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: |
| 261 | + message = "No speech input"; |
| 262 | + break; |
| 263 | + default: |
| 264 | + message = "Didn't understand, please try again."; |
| 265 | + break; |
| 266 | + } |
| 267 | + return message; |
| 268 | + } |
| 269 | +} |
0 commit comments