我正在创建一个应用程序,因为我需要连续的语音识别。但onReadyForSpeech打了两次电话。
我也附上了我的代码。请帮我找出问题所在。 提前谢谢。
private SpeechRecognizer mSpeechRecognizer = null;
public static VoiceRecognizeService sVoiceRecognizeService;
private ITelephony mListener;
private boolean isListening;
private Intent mSaverController;
public VoiceRecognizeService() {
super();
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
sVoiceRecognizeService = this;
startListening();
return START_NOT_STICKY;
}
public void setTelephonyListener(ITelephony mListener) {
this.mListener = mListener;
}
public static VoiceRecognizeService getInstance() {
return sVoiceRecognizeService;
}
// starts the service
public void startListening() {
if (!isListening) {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(this);
Intent mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en_IN");
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 2000);
mRecognizerIntent.putExtra("android.speech.extra.DICTATION_MODE", true);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, false);
mRecognizerIntent.putExtra("android.speech.extra.PREFER_OFFLINE", true);
mRecognizerIntent.putExtra("calling_package", this.getPackageName());
mSpeechRecognizer.startListening(mRecognizerIntent);
isListening = true;
}
}
public void processVoiceCommands(final ArrayList<String> partialData) {
}
public void restartListeningService() {
cancelSpeechRecognition();
startListening();
}
public void cancelSpeechRecognition() {
if (mSpeechRecognizer != null) {
mSpeechRecognizer.stopListening();
mSpeechRecognizer.cancel();
mSpeechRecognizer.destroy();
mSpeechRecognizer = null;
isListening = false;
}
}
@Override
public void onReadyForSpeech(Bundle bundle) {
Log.e("VoiceError", "speechReady");
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float scale) {
if (mListener != null) {
mListener.onRmsChanged(scale);
}
}
@Override
public void onBufferReceived(byte[] bytes) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int i) {
if (i == SpeechRecognizer.ERROR_RECOGNIZER_BUSY) {
} else {
restartListeningService();
}
}
@Override
public void onResults(Bundle bundle) {
final ArrayList<String> data = bundle.getStringArrayList(
SpeechRecognizer.RESULTS_RECOGNITION);
if (data != null) {
processVoiceCommands(data);
}
restartListeningService();
}
@Override
public void onPartialResults(Bundle bundle) {
final ArrayList<String> data = bundle.getStringArrayList(
SpeechRecognizer.RESULTS_RECOGNITION);
Log.e("VoiceError", "partialResults " + data);
}
@Override
public void onEvent(int i, Bundle bundle) {
}
@Override
public void onDestroy() {
if (mSpeechRecognizer != null) {
mSpeechRecognizer.setRecognitionListener(null);
cancelSpeechRecognition();
}
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}