AI2 Component  (Version nb184)
ServiceBasedSpeechRecognizer.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2019 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime;
7 
9 
10 import android.content.Intent;
11 import android.os.Bundle;
12 import android.speech.RecognitionListener;
13 import android.speech.SpeechRecognizer;
14 
15 import java.util.ArrayList;
16 
18  implements RecognitionListener {
19 
20  private SpeechRecognizer speech = null;
21  private Intent recognizerIntent;
22  private ComponentContainer container;
23  private String result;
24 
25  public ServiceBasedSpeechRecognizer(ComponentContainer container, Intent recognizerIntent) {
26  this.container = container;
27  this.recognizerIntent = recognizerIntent;
28  }
29 
30  @Override
31  public void start() {
32  speech = SpeechRecognizer.createSpeechRecognizer(container.$context());
33  speech.setRecognitionListener(this);
34  speech.startListening(recognizerIntent);
35  }
36 
37  @Override
38  public void stop() {
39  if (speech != null) {
40  speech.stopListening();
41  }
42  }
43 
44  @Override
45  public void onReadyForSpeech(Bundle bundle) {
46 
47  }
48 
49  @Override
50  public void onBeginningOfSpeech() {
51 
52  }
53 
54  @Override
55  public void onRmsChanged(float v) {
56 
57  }
58 
59  @Override
60  public void onBufferReceived(byte[] bytes) {
61 
62  }
63 
64  @Override
65  public void onEndOfSpeech() {
66 
67  }
68 
69  @Override
70  public void onError(int i) {
71  int errorNumber = getErrorMessage(i);
72  speechListener.onError(errorNumber);
73  }
74 
75  @Override
76  public void onResults(Bundle bundle) {
77  if (bundle.isEmpty()) {
78  result = "";
79  } else {
80  ArrayList<String> results = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
81  result = results.get(0);
82  }
83  speechListener.onResult(result);
84  }
85 
86  @Override
87  public void onPartialResults(Bundle bundle) {
88  if (bundle.isEmpty()) {
89  result = "";
90  } else {
91  ArrayList<String> results = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
92  result = results.get(0);
93  }
94  speechListener.onPartialResult(result);
95  }
96 
97  @Override
98  public void onEvent(int i, Bundle bundle) {
99 
100  }
101 
102  private int getErrorMessage(int errorCode) {
103  int errCode = ErrorMessages.ERROR_DEFAULT;
104  switch (errorCode) {
105  case SpeechRecognizer.ERROR_AUDIO:
106  errCode = ErrorMessages.ERROR_AUDIO;
107  break;
108  case SpeechRecognizer.ERROR_CLIENT:
109  errCode = ErrorMessages.ERROR_CLIENT;
110  break;
111  case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
113  break;
114  case SpeechRecognizer.ERROR_NETWORK:
115  errCode = ErrorMessages.ERROR_NETWORK;
116  break;
117  case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
119  break;
120  case SpeechRecognizer.ERROR_NO_MATCH:
121  errCode = ErrorMessages.ERROR_NO_MATCH;
122  break;
123  case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
125  break;
126  case SpeechRecognizer.ERROR_SERVER:
127  errCode = ErrorMessages.ERROR_SERVER;
128  break;
129  case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
131  break;
132  }
133  return errCode;
134  }
135 }
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_NETWORK_TIMEOUT
static final int ERROR_NETWORK_TIMEOUT
Definition: ErrorMessages.java:266
com.google.appinventor.components.runtime.SpeechRecognizer
Definition: SpeechRecognizer.java:47
com.google.appinventor.components.runtime.SpeechListener.onPartialResult
void onPartialResult(String text)
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.SpeechListener.onError
void onError(int errorCode)
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer.onBeginningOfSpeech
void onBeginningOfSpeech()
Definition: ServiceBasedSpeechRecognizer.java:50
com.google.appinventor.components
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer.onReadyForSpeech
void onReadyForSpeech(Bundle bundle)
Definition: ServiceBasedSpeechRecognizer.java:45
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer.ServiceBasedSpeechRecognizer
ServiceBasedSpeechRecognizer(ComponentContainer container, Intent recognizerIntent)
Definition: ServiceBasedSpeechRecognizer.java:25
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer.onResults
void onResults(Bundle bundle)
Definition: ServiceBasedSpeechRecognizer.java:76
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_NO_MATCH
static final int ERROR_NO_MATCH
Definition: ErrorMessages.java:267
com.google.appinventor.components.runtime.SpeechRecognizerController
Definition: SpeechRecognizerController.java:11
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_RECOGNIZER_BUSY
static final int ERROR_RECOGNIZER_BUSY
Definition: ErrorMessages.java:268
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_DEFAULT
static final int ERROR_DEFAULT
Definition: ErrorMessages.java:18
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer.onBufferReceived
void onBufferReceived(byte[] bytes)
Definition: ServiceBasedSpeechRecognizer.java:60
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer.onPartialResults
void onPartialResults(Bundle bundle)
Definition: ServiceBasedSpeechRecognizer.java:87
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer.stop
void stop()
Definition: ServiceBasedSpeechRecognizer.java:38
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer.start
void start()
Definition: ServiceBasedSpeechRecognizer.java:31
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer.onEvent
void onEvent(int i, Bundle bundle)
Definition: ServiceBasedSpeechRecognizer.java:98
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_AUDIO
static final int ERROR_AUDIO
Definition: ErrorMessages.java:262
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_INSUFFICIENT_PERMISSIONS
static final int ERROR_INSUFFICIENT_PERMISSIONS
Definition: ErrorMessages.java:264
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_NETWORK
static final int ERROR_NETWORK
Definition: ErrorMessages.java:265
com.google
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer.onError
void onError(int i)
Definition: ServiceBasedSpeechRecognizer.java:70
com
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_SPEECH_TIMEOUT
static final int ERROR_SPEECH_TIMEOUT
Definition: ErrorMessages.java:270
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer
Definition: ServiceBasedSpeechRecognizer.java:17
com.google.appinventor.components.runtime.SpeechListener.onResult
void onResult(String text)
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer.onRmsChanged
void onRmsChanged(float v)
Definition: ServiceBasedSpeechRecognizer.java:55
com.google.appinventor.components.runtime.ServiceBasedSpeechRecognizer.onEndOfSpeech
void onEndOfSpeech()
Definition: ServiceBasedSpeechRecognizer.java:65
com.google.appinventor
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_SERVER
static final int ERROR_SERVER
Definition: ErrorMessages.java:269
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_CLIENT
static final int ERROR_CLIENT
Definition: ErrorMessages.java:263