我想在Android上使用SpeechRecognizer类并影响它的RecognitionListener进行语音识别。Android监听器语音识别不叫
问题:受影响的侦听器未被调用。
这里是我的主要活动代码:
package com.example.testvoicepaper;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
public int VOICE_RECOGNITION_REQUEST_CODE = 1;
private Button launchRecognition;
private TextView textRecognised;
private ImageView picture;
private SpeechRecognizer sr;
private Context context;
private MyRecognitionListener listener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textRecognised = (TextView) findViewById(R.id.textRecognised);
launchRecognition = (Button) findViewById(R.id.launchRecognition);
picture = (ImageView) findViewById(R.id.picture);
context = this;
sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
listener = new MyRecognitionListener();
sr.setRecognitionListener(listener);
launchRecognition.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
sr.startListening(RecognizerIntent.getVoiceDetailsIntent(context));
listener.onBeginningOfSpeech();
break;
}
case MotionEvent.ACTION_UP: {
listener.onEndOfSpeech();
sr.stopListening();
break;
}
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class MyRecognitionListener implements RecognitionListener {
@Override
public void onBeginningOfSpeech() {
Log.d("Speech", "onBeginningOfSpeech");
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.d("Speech", "onBufferReceived");
}
@Override
public void onEndOfSpeech() {
Log.d("Speech", "onEndOfSpeech");
}
@Override
public void onError(int error) {
Log.d("Speech", "onError" + error);
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.d("Speech", "onEvent");
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.d("Speech", "onPartialResults");
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.d("Speech", "onReadyForSpeech");
}
@Override
public void onResults(Bundle results) {
Log.d("Speech", "onResults");
ArrayList strlist = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < strlist.size();i++) {
Log.d("Speech", "result=" + strlist.get(i));
}
}
@Override
public void onRmsChanged(float rmsdB) {
Log.d("Speech", "onRmsChanged");
}
}
}
它调用beginningOfSpeech()和endOfSpeech(),但从来没有监听功能。
+0
你有答案吗?我有同样的问题。 –