2012-07-25 49 views
3

我想了解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

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