我想在我的Android应用程序中引入永久语音识别功能。
我知道语音识别在Android 4.1.1和4.2上不时冻结,因此我建立了一个计时器,我不时检查语音识别是否仍然存在,如果它不活着我停止它然后开始它。不幸的是,当它试图重新启动语音识别器时,我会得到类似的结果:
SpeechRecognizer not connected to the recognition service
并且在onError回调中我收到错误8(文档说:ERROR_RECOGNIZER_BUSY),即使我在启动语音识别器之前停止每个计时器。
代码如下所示:
public class MainActivity extends Activity {
private SpeechRecognizer mSpeechRecognizer;
private RecognitionListener mRecognitionListener;
private Intent mi;
private boolean isSpeechRecognizerAlive;
Timer myTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mi = new Intent(getApplicationContext(), MyService.class);
mRecognitionListener = new RecognitionListener(){
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onResults(Bundle results) {
Log.e("recognizer listener", "onResults");
ArrayList<String> result = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for(int i=0;i<result.size();i++){
Log.e(String.valueOf(i), result.get(i));
}
startRecognition(new View(getApplicationContext()));
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.e("recognizer listener", "onReadyForSpeech");
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.e("recognizer listener", "onPartialResults");
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.e("recognizer listener", "onEvent");
}
@Override
public void onError(int error) {
Log.e("recognizer listener", "onError: " + String.valueOf(error));
isSpeechRecognizerAlive = false;
}
@Override
public void onEndOfSpeech() {
Log.e("recognizer listener", "onEndOfSpeech");
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.e("recognizer listener", "onBufferReceived");
}
@Override
public void onBeginningOfSpeech() {
Log.e("recognizer listener", "onBeginningOfSpeech");
isSpeechRecognizerAlive = true;
}
};
new CheckRecognizer().execute("");
}
private void checkIfRecognizerAslive(){
Log.e("check", "check");
if(!isSpeechRecognizerAlive){
Log.e("check1", "check1");
stopRecognition(new View(getApplicationContext()));
startRecognition(new View(getApplicationContext()));
}
}
@Override
protected void onResume() {
super.onResume();
Log.e("Start", "service");
}
@Override
protected void onPause() {
super.onPause();
Log.e("Stop", "service");
}
public void startRecognition(View view){
Log.e("MainActivity", "startRecognition");
isSpeechRecognizerAlive = false;
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
mSpeechRecognizer.setRecognitionListener(mRecognitionListener);
mSpeechRecognizer.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
}
public void stopRecognition(View view){
Log.e("MainActivity", "stopRecognition");
if(mSpeechRecognizer != null){
mSpeechRecognizer.stopListening();
mSpeechRecognizer.cancel();
mSpeechRecognizer.destroy();
mSpeechRecognizer = null;
}
}
private class CheckRecognizer extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
publishProgress();
}
}, 0, 4000);
return "";
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {
checkIfRecognizerAslive();
}
}
}
我真的不知道我做错了什么。你能帮我找到问题吗?或者有人知道为什么我的语音识别器忙碌,即使我摧毁它?任何建议都是受欢迎的。如果您对如何在Android上进行永久语音识别有其他建议,那将会非常棒。非常感谢!