[英]Speech Recognition Android App
我正在制作一個應用程序,它接收來自用戶的命令並實時編寫。 什么是我最好的選擇? 像sphinx這樣的第三方軟件還是應該使用內置的(android語音識別)?
其次我想讓它實時寫,就像我說它開始寫作一樣?
您應該使用內置的Android語音識別功能。 具體來說,您需要操作SpeechRecognier API,以便沒有彈出對話框。
另外,不要指望SpeechRecognizer在onPartialResults()中返回任何內容。 它很少。
您可以嘗試使用Sphinx,但似乎其他開發人員難以在Android上運行它。 也就是說,如果您希望您的應用在沒有互聯網連接的情況下運行,那么sphinx將是您唯一的選擇。
以下是使用SpeechRecognizer所需的代碼片段:
public void recognizeDirectly(Intent recognizerIntent)
{
// SpeechRecognizer requires EXTRA_CALLING_PACKAGE, so add if it's not
// here
if (!recognizerIntent.hasExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE))
{
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
"com.dummy");
}
SpeechRecognizer recognizer = getSpeechRecognizer();
recognizer.startListening(recognizerIntent);
}
@Override
public void onResults(Bundle results)
{
Log.d(TAG, "full results");
receiveResults(results);
}
@Override
public void onPartialResults(Bundle partialResults)
{
Log.d(TAG, "partial results");
receiveResults(partialResults);
}
/**
* common method to process any results bundle from {@link SpeechRecognizer}
*/
private void receiveResults(Bundle results)
{
if ((results != null)
&& results.containsKey(SpeechRecognizer.RESULTS_RECOGNITION))
{
List<String> heard =
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
float[] scores =
results.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES);
receiveWhatWasHeard(heard, scores);
}
}
@Override
public void onError(int errorCode)
{
recognitionFailure(errorCode);
}
/**
* stop the speech recognizer
*/
@Override
protected void onPause()
{
if (getSpeechRecognizer() != null)
{
getSpeechRecognizer().stopListening();
getSpeechRecognizer().cancel();
getSpeechRecognizer().destroy();
}
super.onPause();
}
/**
* lazy initialize the speech recognizer
*/
private SpeechRecognizer getSpeechRecognizer()
{
if (recognizer == null)
{
recognizer = SpeechRecognizer.createSpeechRecognizer(this);
recognizer.setRecognitionListener(this);
}
return recognizer;
}
// other unused methods from RecognitionListener...
@Override
public void onReadyForSpeech(Bundle params)
{
Log.d(TAG, "ready for speech " + params);
}
@Override
public void onEndOfSpeech()
{
}
gregm是正確的,但問題的主要“實時寫入”部分沒有得到回答。 您需要添加一個額外的內容,以表明您有興趣獲取部分結果:
添加額外的意圖對我有用
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
警告:部分不僅返回新內容,而且還返回前一個內容。 所以你需要自己檢查差異...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.