我正在制作一个应用程序,它从用户那里获取命令并实时编写。对我来说最好的选择是什么?像 sphinx 这样的第三方软件还是我应该使用内置的(android 语音识别)?
其次我想让它实时写作,就像我说话时它开始写作一样?
最佳答案
您应该使用内置的 Android 语音识别功能。具体来说,您需要操作 SpeechRecognier不弹出对话框的API。
此外,不要指望 SpeechRecognizer 在 onPartialResults() 内返回任何内容.它很少这样做。
您可以尝试使用 Sphinx,但其他开发人员似乎很难让它在 Android 上运行。也就是说,如果您希望您的应用程序在没有互联网连接的情况下运行,那么 sphinx 将是您唯一的选择。
这是您需要使用 SpeechRecognizer 的代码 fragment :
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()
{
}
关于android - 语音识别安卓应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9625045/