我有一个 maind UI 类,其中有一个按钮实例化一个类,该类正在实现 SpeechRecognizer 库以将语音从文本转换为文本。
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startNoiseProcessService();
}
});
startNoiseProcessService() 是一个调用另一个类对象的函数。
public void startNoiseProcessService() {
StreamService ss = new StreamService(this);
String s = ss.startStreaming();
if ( s.equals("NA") ) {
Toast t = Toast.makeText(getApplicationContext(),threadCnt +
"Opps! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
StreamService 类实现 SpeechRecognizer API。
这个 StreamService 类被完全调用,问题在于,在通过 onResults(Bundle)
方法计算结果后,我无法从语音中获取转换后的文本。
我需要转换后的文本来更新我的文本框,但我没有得到它。
startStreaming() 实现:
public String startStreaming() {
text = "";
if ( !SpeechRecognizer.isRecognitionAvailable(context) ) {
text = "NA";
return text;
}
Log.d(TAG, "started taking input");
sr = SpeechRecognizer.createSpeechRecognizer(context);
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
sr.setRecognitionListener( new mylistener());
sr.startListening(intent);
}
最佳答案
根据您的评论,我认为您在调用 sr.startListening(intent)
时无法获得结果。您需要使用监听器的 onResults
方法获取结果,并从那里处理来自 Bundle
内部的文本。
我实现这一点的方法是让一个Service
负责处理SpeechRecognizer
。然后,当调用 onResults
时,我使用消息处理程序将文本发送回原始调用者。您可以在此处查看有关如何执行此操作的详细信息:https://developer.android.com/training/multiple-threads/communicate-ui.html
关于java - 没有从 SpeechRecognizer 类获得结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20517152/