2017-04-06 50 views
-2

美好的一天,Android的語音識別/聽寫

我正處於建立烹飪/食譜應用程序的早期階段。該應用程序的主要目的是能夠使用語音聽寫跟蹤和遍歷食譜。任何人都可以指出我如何實現這些功能的正確方向?

謝謝!

回答

1

調用系統的內置語音識別器活動來獲取用戶的語音輸入。這對從用戶獲得輸入並進行處理很有用,例如執行搜索或將其作爲消息發送。

在您的應用中,您可以使用ACTION_RECOGNIZE_SPEECH操作調用startActivityForResult()。這將啓動語音識別活動,然後您可以在onActivityResult()中處理結果。

private static final int SPEECH_REQUEST_CODE = 0; 

// Create an intent that can start the Speech Recognizer activity 
private void displaySpeechRecognizer() { 
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
// Start the activity, the intent will be populated with the speech text 
    startActivityForResult(intent, SPEECH_REQUEST_CODE); 
} 

// This callback is invoked when the Speech Recognizer returns. 
// This is where you process the intent and extract the speech text from the intent. 
@Override 
protected void onActivityResult(int requestCode, int resultCode, 
     Intent data) { 
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) { 
     List<String> results = data.getStringArrayListExtra(
       RecognizerIntent.EXTRA_RESULTS); 
     String spokenText = results.get(0); 
     // Do something with spokenText 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
}