了解语音识别服务

时间:2012-07-25 15:15:34

标签: android android-service voice-recognition

我想了解RecognitionServiceRecognitionService.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);

有人可以帮助我让这个工作。如果有人可以指点我这方面的教程,或描述如何做的一般流程,那将是很棒的。

非常感谢。

1 个答案:

答案 0 :(得分:1)

基本想法是使用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