了解语音识别服务
问题描述:
我想了解RecognitionService和RecognitionService.Callback的功能。我对这个框架相当陌生,并且想知道如何在RecognitionService中调用onStartListening()函数。我看到帖子How to register a custom speech recognition service?,但是我已经在所有主要函数中插入了日志消息,以查看哪个函数在什么时候被调用。了解语音识别服务
我也看了一下sdk中的示例应用程序,但它解释了事情发生的过程中做得很糟糕。我想从一个活动中调用startService。
我用下面的意图
Intent startServiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
startServiceIntent.setClass(this, SimpleVoiceService.class);
startService(startServiceIntent);
可能有人请帮助我得到这个工作。如果有人能指点我一个关于这个的教程,或者描述如何做到这一点的一般流程,那将是非常棒的。
非常感谢。
答
基本想法是使用SpeechRecognizer
连接到用户在一般Android设置中选择的RecognitionService
。
SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(context);
sr.setRecognitionListener(new RecognitionListener() {
@Override
public void onResults(Bundle b) { /* ... */ }
// other required methods
});
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "");
sr.startListening(intent);
您必须提供RecognitionListener
- 方法的实现,允许你以响应语音识别的事件更新UI(用户开始说话,局部结果可用,用户停止讲话,录制还在进行,发生错误等)。
查看某些键盘应用程序的源代码的完整实现,例如VoiceInput class in Hacker's Keyboard。