public class SpeechRecognizer
extends Object
| java.lang.Object | |
| android.speech.SpeechRecognizer | |
此课程提供对语音识别服务的访问。 该服务允许访问语音识别器。 不要直接实例化这个类,而应该调用createSpeechRecognizer(Context) 。 这个类的方法只能从主应用程序线程中调用。
这个API的实现可能会将音频传输到远程服务器以执行语音识别。 因此,此API不打算用于连续识别,这会消耗大量的电池和带宽。
请注意,该应用程序必须具有 RECORD_AUDIO权限才能使用此类。
常量(Constants) |
|
|---|---|
String |
CONFIDENCE_SCORES 用于从 |
int |
ERROR_AUDIO 录音错误。 |
int |
ERROR_CLIENT 其他客户端错误。 |
int |
ERROR_INSUFFICIENT_PERMISSIONS 权限不足 |
int |
ERROR_NETWORK 其他网络相关的错误。 |
int |
ERROR_NETWORK_TIMEOUT 网络操作超时。 |
int |
ERROR_NO_MATCH 没有识别结果匹配。 |
int |
ERROR_RECOGNIZER_BUSY RecognitionService忙。 |
int |
ERROR_SERVER 服务器发送错误状态。 |
int |
ERROR_SPEECH_TIMEOUT 没有语音输入 |
String |
RESULTS_RECOGNITION 主要用于取得 |
公共方法(Public methods) |
|
|---|---|
void |
cancel() 取消语音识别。 |
static SpeechRecognizer |
createSpeechRecognizer(Context context) 工厂方法创建一个新的 |
static SpeechRecognizer |
createSpeechRecognizer(Context context, ComponentName serviceComponent) 工厂方法创建一个新的 |
void |
destroy() 销毁 |
static boolean |
isRecognitionAvailable(Context context) 检查系统上是否有语音识别服务。 |
void |
setRecognitionListener(RecognitionListener listener) 设置将接收所有回调的侦听器。 |
void |
startListening(Intent recognizerIntent) 开始倾听演讲。 |
void |
stopListening() 停止收听演讲。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
String CONFIDENCE_SCORES
用于从Bundle检索传递给onResults(Bundle)和onPartialResults(Bundle)方法的浮点数组的Bundle 。 该数组的大小应与RESULTS_RECOGNITION提供的ArrayList的大小相同,并应包含范围从0.0到1.0的值或-1以表示不可用的置信度分数。
置信度值接近1.0表示置信度较高(语音识别器确信识别结果正确),而接近0.0的值表示置信度较低。
该值是可选的,可能不会提供。
常数值:“confidence_scores”
int ERROR_INSUFFICIENT_PERMISSIONS
权限不足
常量值:9(0x00000009)
int ERROR_RECOGNIZER_BUSY
RecognitionService忙。
常量值:8(0x00000008)
String RESULTS_RECOGNITION
主要用于取得ArrayList<String>从Bundle传递到onResults(Bundle)种onPartialResults(Bundle)方法。 这些字符串是可能的识别结果,其中第一个元素是最有可能的候选者。
常量值:“results_recognition”
void cancel ()
取消语音识别。 请注意,应该事先调用setRecognitionListener(RecognitionListener) ,否则不会收到通知。
SpeechRecognizer createSpeechRecognizer (Context context)
工厂方法创建一个新的SpeechRecognizer 。 请注意: setRecognitionListener(RecognitionListener)应该派遣任何命令来创建之前被调用SpeechRecognizer ,否则任何通知将被接收。
| 参数(Parameters) | |
|---|---|
context |
Context: in which to create SpeechRecognizer |
| 返回(Returns) | |
|---|---|
SpeechRecognizer |
a new SpeechRecognizer |
SpeechRecognizer createSpeechRecognizer (Context context, ComponentName serviceComponent)
工厂方法创建一个新的SpeechRecognizer 。 请注意: setRecognitionListener(RecognitionListener)应该派遣任何命令来创建之前被调用SpeechRecognizer ,否则任何通知将被接收。 使用此版本的方法来指定一个特定的服务来指示此SpeechRecognizer 。 通常你不会使用它; 改为使用createSpeechRecognizer(Context)来使用系统默认识别服务。
| 参数(Parameters) | |
|---|---|
context |
Context: in which to create SpeechRecognizer |
serviceComponent |
ComponentName: the ComponentName of a specific service to direct this SpeechRecognizer to |
| 返回(Returns) | |
|---|---|
SpeechRecognizer |
a new SpeechRecognizer |
boolean isRecognitionAvailable (Context context)
检查系统上是否有语音识别服务。 如果此方法返回false ,则createSpeechRecognizer(Context)将失败。
| 参数(Parameters) | |
|---|---|
context |
Context: with which SpeechRecognizer will be created |
| 返回(Returns) | |
|---|---|
boolean |
true if recognition is available, false otherwise |
void setRecognitionListener (RecognitionListener listener)
设置将接收所有回调的侦听器。 以前未完成的命令将与旧侦听器一起执行,而任何后续命令将与新侦听器一起执行。
| 参数(Parameters) | |
|---|---|
listener |
RecognitionListener: listener that will receive all the callbacks from the created SpeechRecognizer, this must not be null. |
void startListening (Intent recognizerIntent)
开始倾听演讲。 请注意,应该事先调用setRecognitionListener(RecognitionListener) ,否则不会收到通知。
| 参数(Parameters) | |
|---|---|
recognizerIntent |
Intent: 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. |
void stopListening ()
停止收听演讲。 到目前为止捕获的语音将被识别为用户此时已经停止发言。 请注意,在默认情况下,这不需要调用,因为当语音结束指针确定语音完成时,语音结束指针会自动停止识别器的侦听。 但是,您可以使用RecognizerIntent中定义的意图附加功能直接操作endpointer参数,在这种情况下,您有时可能需要手动调用此方法以尽快停止监听。 请注意,应该事先调用setRecognitionListener(RecognitionListener) ,否则不会收到通知。