AI2 Component  (Version nb184)
SpeechRecognizer.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2019 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
20 
21 import android.content.Intent;
22 import android.Manifest;
23 import android.os.Build;
24 import android.speech.RecognizerIntent;
25 
38 @DesignerComponent(version = YaVersion.SPEECHRECOGNIZER_COMPONENT_VERSION,
39  description = "Component for using Voice Recognition to convert from speech to text",
40  category = ComponentCategory.MEDIA,
41  nonVisible = true,
42  iconName = "images/speechRecognizer.png")
43 
44 @SimpleObject
45 @UsesPermissions(permissionNames = "android.permission.RECORD_AUDIO," +
46  "android.permission.INTERNET")
49 
50  private final ComponentContainer container;
51  private String result;
52  private Intent recognizerIntent;
53  private SpeechRecognizerController speechRecognizerController;
54 
55  private boolean havePermission = false;
56  private boolean useLegacy = true;
57 
64  super(container.$form());
65  container.$form().registerForOnClear(this);
66  this.container = container;
67  result = "";
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);
71  UseLegacy(useLegacy);
72  }
73 
78  category = PropertyCategory.BEHAVIOR)
79  public String Result() {
80  return result;
81  }
82 
88  public void GetText() {
89  // Need to check if we have RECORD_AUDIO permission
90  if (!havePermission) {
91  final SpeechRecognizer me = this;
92  form.runOnUiThread(new Runnable() {
93  @Override
94  public void run() {
95  form.askPermission(Manifest.permission.RECORD_AUDIO,
97  @Override
98  public void HandlePermissionResponse(String permission, boolean granted) {
99  if (granted) {
100  me.havePermission = true;
101  me.GetText();
102  } else {
103  form.dispatchPermissionDeniedEvent(me, "GetText", Manifest.permission.RECORD_AUDIO);
104  }
105  }
106  });
107  }
108  });
109  return;
110  }
111  BeforeGettingText();
112  speechRecognizerController.addListener(this);
113  speechRecognizerController.start();
114  }
115 
123  public void Stop() {
124  if (speechRecognizerController != null) {
125  speechRecognizerController.stop();
126  }
127  }
128 
133  @SimpleEvent
134  public void BeforeGettingText() {
135  EventDispatcher.dispatchEvent(this, "BeforeGettingText");
136  }
137 
147  @SimpleEvent
148  public void AfterGettingText(String result, boolean partial) {
149  EventDispatcher.dispatchEvent(this, "AfterGettingText", result, partial);
150  }
151 
155  @Override
156  public void onPartialResult(String text) {
157  result = text;
158  AfterGettingText(result, true);
159  }
160 
164  @Override
165  public void onResult(String text) {
166  result = text;
167  AfterGettingText(result, false);
168  }
169 
173  @Override
174  public void onError(int errorNumber) {
175  String functionName = "GetText";
176  form.dispatchErrorOccurredEvent(this, functionName, errorNumber);
177  }
178 
179  @Override
180  public void onClear() {
181  Stop();
182  speechRecognizerController = null;
183  recognizerIntent = null;
184  }
185 
187  category = PropertyCategory.BEHAVIOR,
188  description = "If true, an app can retain their older behaviour.")
189  public boolean UseLegacy() {
190  return useLegacy;
191  }
192 
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;
210  Stop();
211  if (useLegacy == true || Build.VERSION.SDK_INT<8) {
212  speechRecognizerController = new IntentBasedSpeechRecognizer(container, recognizerIntent);
213  } else {
214  speechRecognizerController = new ServiceBasedSpeechRecognizer(container, recognizerIntent);
215  }
216  }
217 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.SpeechRecognizer
Definition: SpeechRecognizer.java:47
com.google.appinventor.components.runtime.SpeechRecognizer.onResult
void onResult(String text)
Definition: SpeechRecognizer.java:165
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.runtime.SpeechRecognizer.SpeechRecognizer
SpeechRecognizer(ComponentContainer container)
Definition: SpeechRecognizer.java:63
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.runtime.SpeechRecognizer.onError
void onError(int errorNumber)
Definition: SpeechRecognizer.java:174
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.runtime.SpeechRecognizer.onPartialResult
void onPartialResult(String text)
Definition: SpeechRecognizer.java:156
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.annotations.UsesPermissions
Definition: UsesPermissions.java:21
com.google.appinventor.components.runtime.SpeechRecognizer.onClear
void onClear()
Definition: SpeechRecognizer.java:180
com.google.appinventor.components.runtime.SpeechRecognizerController
Definition: SpeechRecognizerController.java:11
com.google.appinventor.components.runtime.PermissionResultHandler
Definition: PermissionResultHandler.java:15
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.SpeechRecognizer.GetText
void GetText()
Definition: SpeechRecognizer.java:88
com.google.appinventor.components.runtime.OnClearListener
Definition: OnClearListener.java:16
com.google.appinventor.components.runtime.AndroidNonvisibleComponent
Definition: AndroidNonvisibleComponent.java:17
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.runtime.SpeechListener
Definition: SpeechListener.java:12
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.SpeechRecognizer.Stop
void Stop()
Definition: SpeechRecognizer.java:123
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google
com
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer
Definition: ServiceBasedSpeechRecognizer.java:17
com.google.appinventor.components.runtime.IntentBasedSpeechRecognizer
Definition: IntentBasedSpeechRecognizer.java:14
com.google.appinventor.components.runtime.SpeechRecognizer.AfterGettingText
void AfterGettingText(String result, boolean partial)
Definition: SpeechRecognizer.java:148
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.SpeechRecognizer.BeforeGettingText
void BeforeGettingText()
Definition: SpeechRecognizer.java:134
com.google.appinventor.components.runtime.Form.registerForOnClear
void registerForOnClear(OnClearListener component)
Definition: Form.java:797
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor