7 package com.google.appinventor.components.runtime;
21 import android.content.Intent;
22 import android.Manifest;
23 import android.os.Build;
24 import android.speech.RecognizerIntent;
38 @DesignerComponent(version = YaVersion.SPEECHRECOGNIZER_COMPONENT_VERSION,
39 description =
"Component for using Voice Recognition to convert from speech to text",
40 category = ComponentCategory.MEDIA,
42 iconName =
"images/speechRecognizer.png")
45 @UsesPermissions(permissionNames =
"android.permission.RECORD_AUDIO," +
46 "android.permission.INTERNET")
51 private String result;
52 private Intent recognizerIntent;
55 private boolean havePermission =
false;
56 private boolean useLegacy =
true;
64 super(container.
$form());
66 this.container = container;
68 recognizerIntent =
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
69 recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
70 recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,
true);
79 public String Result() {
90 if (!havePermission) {
92 form.runOnUiThread(
new Runnable() {
95 form.askPermission(Manifest.permission.RECORD_AUDIO,
98 public void HandlePermissionResponse(String permission,
boolean granted) {
100 me.havePermission =
true;
103 form.dispatchPermissionDeniedEvent(me,
"GetText", Manifest.permission.RECORD_AUDIO);
112 speechRecognizerController.addListener(
this);
113 speechRecognizerController.start();
124 if (speechRecognizerController !=
null) {
125 speechRecognizerController.stop();
158 AfterGettingText(result,
true);
167 AfterGettingText(result,
false);
175 String functionName =
"GetText";
176 form.dispatchErrorOccurredEvent(
this, functionName, errorNumber);
182 speechRecognizerController =
null;
183 recognizerIntent =
null;
188 description =
"If true, an app can retain their older behaviour.")
189 public
boolean UseLegacy() {
204 defaultValue =
"True")
205 @
SimpleProperty(description =
"If true, a separate dialog is used to recognize speech "
206 +
"(the default). If false, speech is recognized in the background and "
207 +
"partial results are also provided.")
208 public
void UseLegacy(
boolean useLegacy) {
209 this.useLegacy = useLegacy;
211 if (useLegacy ==
true || Build.VERSION.SDK_INT<8) {