public class SpeechRecognizer extends Object
createSpeechRecognizer(Context)
. This class's methods must be
invoked only from the main application thread.
The implementation of this API is likely to stream audio to remote servers to perform speech recognition. As such this API is not intended to be used for continuous recognition, which would consume a significant amount of battery and bandwidth.
Please note that the application must have android.Manifest.permission#RECORD_AUDIO
permission to use this class.
Modifier and Type | Field and Description |
---|---|
static String |
CONFIDENCE_SCORES
Key used to retrieve a float array from the
Bundle passed to the
RecognitionListener.onResults(Bundle) and
RecognitionListener.onPartialResults(Bundle) methods. |
static int |
ERROR_AUDIO
Audio recording error.
|
static int |
ERROR_CLIENT
Other client side errors.
|
static int |
ERROR_INSUFFICIENT_PERMISSIONS
Insufficient permissions
|
static int |
ERROR_NETWORK
Other network related errors.
|
static int |
ERROR_NETWORK_TIMEOUT
Network operation timed out.
|
static int |
ERROR_NO_MATCH
No recognition result matched.
|
static int |
ERROR_RECOGNIZER_BUSY
RecognitionService busy.
|
static int |
ERROR_SERVER
Server sends error status.
|
static int |
ERROR_SPEECH_TIMEOUT
No speech input
|
static String |
RESULTS_RECOGNITION
Key used to retrieve an
ArrayList<String> from the Bundle passed to the
RecognitionListener.onResults(Bundle) and
RecognitionListener.onPartialResults(Bundle) methods. |
Modifier and Type | Method and Description |
---|---|
void |
cancel()
Cancels the speech recognition.
|
static SpeechRecognizer |
createSpeechRecognizer(Context context)
Factory method to create a new
SpeechRecognizer . |
static SpeechRecognizer |
createSpeechRecognizer(Context context,
ComponentName serviceComponent)
Factory method to create a new
SpeechRecognizer . |
void |
destroy()
Destroys the
SpeechRecognizer object. |
static boolean |
isRecognitionAvailable(Context context)
Checks whether a speech recognition service is available on the system.
|
void |
setRecognitionListener(RecognitionListener listener)
Sets the listener that will receive all the callbacks.
|
void |
startListening(Intent recognizerIntent)
Starts listening for speech.
|
void |
stopListening()
Stops listening for speech.
|
public static final String RESULTS_RECOGNITION
ArrayList<String>
from the Bundle
passed to the
RecognitionListener.onResults(Bundle)
and
RecognitionListener.onPartialResults(Bundle)
methods. These strings are the possible
recognition results, where the first element is the most likely candidate.public static final String CONFIDENCE_SCORES
Bundle
passed to the
RecognitionListener.onResults(Bundle)
and
RecognitionListener.onPartialResults(Bundle)
methods. The array should be
the same size as the ArrayList provided in RESULTS_RECOGNITION
, and should contain
values ranging from 0.0 to 1.0, or -1 to represent an unavailable confidence score.
Confidence values close to 1.0 indicate high confidence (the speech recognizer is confident that the recognition result is correct), while values close to 0.0 indicate low confidence.
This value is optional and might not be provided.
public static final int ERROR_NETWORK_TIMEOUT
public static final int ERROR_NETWORK
public static final int ERROR_AUDIO
public static final int ERROR_SERVER
public static final int ERROR_CLIENT
public static final int ERROR_SPEECH_TIMEOUT
public static final int ERROR_NO_MATCH
public static final int ERROR_RECOGNIZER_BUSY
public static final int ERROR_INSUFFICIENT_PERMISSIONS
public static boolean isRecognitionAvailable(Context context)
false
, createSpeechRecognizer(Context)
will
fail.context
- with which SpeechRecognizer
will be createdtrue
if recognition is available, false
otherwisepublic static SpeechRecognizer createSpeechRecognizer(Context context)
SpeechRecognizer
. Please note that
setRecognitionListener(RecognitionListener)
should be called before dispatching any
command to the created SpeechRecognizer
, otherwise no notifications will be
received.context
- in which to create SpeechRecognizer
SpeechRecognizer
public static SpeechRecognizer createSpeechRecognizer(Context context, ComponentName serviceComponent)
SpeechRecognizer
. Please note that
setRecognitionListener(RecognitionListener)
should be called before dispatching any
command to the created SpeechRecognizer
, otherwise no notifications will be
received.
Use this version of the method to specify a specific service to direct this
SpeechRecognizer
to. Normally you would not use this; use
createSpeechRecognizer(Context)
instead to use the system default recognition
service.context
- in which to create SpeechRecognizer
serviceComponent
- the ComponentName
of a specific service to direct this
SpeechRecognizer
toSpeechRecognizer
public void setRecognitionListener(RecognitionListener listener)
listener
- listener that will receive all the callbacks from the created
SpeechRecognizer
, this must not be null.public void startListening(Intent recognizerIntent)
setRecognitionListener(RecognitionListener)
should be called beforehand, otherwise
no notifications will be received.recognizerIntent
- contains parameters for the recognition to be performed. The intent
may also contain optional extras, see RecognizerIntent
. If these values are
not set explicitly, default values will be used by the recognizer.public void stopListening()
RecognizerIntent
, in which case you may sometimes
want to manually call this method to stop listening sooner. Please note that
setRecognitionListener(RecognitionListener)
should be called beforehand, otherwise
no notifications will be received.public void cancel()
setRecognitionListener(RecognitionListener)
should be called beforehand, otherwise
no notifications will be received.public void destroy()
SpeechRecognizer
object.