我开始任何活动后,我的识别器都无法工作。它不起作用。我已经实现了RecognitionListener
接口。我的代码如下。(这里说如果您有任何错误或启动活动时会发生什么,并发布您的logcat消息)
public class Main_Activity extends AppCompatActivity implements RecognitionListener {
private TextToSpeech tts;
private TextView returnedText;
private ToggleButton toggleButton;
private ProgressBar progressBar;
private SpeechRecognizer speech = null;
private Intent recognizerIntent;
private Handler handler;
private String LOG_TAG = "VoiceRecognitionActivity";
private static final int REQUEST_CODE = 1;
private AccesingContacts_Class contacts;
private TextToSpeech_Activity speaking;
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_);
returnedText = (TextView) findViewById(R.id.textView1);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
progressBar.setVisibility(View.INVISIBLE);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
if (speech == null) {
speech = SpeechRecognizer.createSpeechRecognizer(Main_Activity.this);
speech.setRecognitionListener(Main_Activity.this);
}
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS,5000);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, Main_Activity.this.getPackageName());
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
speech.startListening(recognizerIntent);
} else {
progressBar.setIndeterminate(false);
progressBar.setVisibility(View.INVISIBLE);
speech.stopListening();
}
}
});
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
public void listen() {
if (speech == null) {
speech = SpeechRecognizer.createSpeechRecognizer(this);
speech.setRecognitionListener(this);
}
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS,5000);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
speech.startListening(recognizerIntent);
} else {
progressBar.setIndeterminate(false);
progressBar.setVisibility(View.INVISIBLE);
speech.stopListening();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Bluetooth:
Intent i = new Intent(this, Bluetooth_Demo.class);
this.startActivity(i);
return true;
case R.id.TextToSpeech:
Intent in = new Intent(this, TextToSpeech_Activity.class);
this.startActivity( in );
return true;
case R.id.StartService:
//startService(new Intent(getBaseContext(),Recognition_Service.class));
return true;
case R.id.StopService:
//stopService(new Intent(getBaseContext(),Recognition_Service.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
if (speech != null) {
speech.destroy();
Log.i(LOG_TAG, "destroy");
}
}
@Override
public void onBeginningOfSpeech() {
Log.i(LOG_TAG, "onBeginningOfSpeech");
progressBar.setIndeterminate(false);
progressBar.setMax(10);
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.i(LOG_TAG, "onBufferReceived: " + buffer);
}
@Override
public void onEndOfSpeech() {
Log.i(LOG_TAG, "onEndOfSpeech");
progressBar.setIndeterminate(true);
toggleButton.setChecked(false);
}
@Override
public void onError(int errorCode) {
String errorMessage = getErrorText(errorCode);
Log.d(LOG_TAG, "FAILED " + errorMessage);
returnedText.setText(errorMessage);
toggleButton.setChecked(false);
listen();
}
@Override
public void onEvent(int arg0, Bundle arg1) {
Log.i(LOG_TAG, "onEvent");
}
@Override
public void onPartialResults(Bundle arg0) {
Log.i(LOG_TAG, "onPartialResults");
}
@Override
public void onReadyForSpeech(Bundle arg0) {
Log.i(LOG_TAG, "onReadyForSpeech");
}
@Override
public void onResults(Bundle results) {
Log.i(LOG_TAG, "onResults");
ArrayList < String > matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String text = "";
for (String result: matches)
text += result + "\n";
returnedText.setText(text);
if (matches.contains("open camera") || matches.contains("on camera") || matches.contains("zora open camera")) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent cmintent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
texttospeech("Opening Camera");
startActivity(cmintent);
returnedText.setText("");
}
}, 2000);
listen();
} else if (matches.contains("take picture")) {
texttospeech("Taking Picture");
Intent cameraintent = new Intent(this, TakingPictures_class.class);
startActivity(cameraintent);
listen();
} else if (matches.contains("open message")) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("vnd.android-dir/mms-sms");
returnedText.setText("");
startActivity(intent);
listen();
} else if (matches.contains("write message")) {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
matches.clear();
speaking.speakWords("whom you wanna send the message");
String name = matches.get(0).toString();
contacts = new AccesingContacts_Class();
String number = contacts.getContactNumber(this, name);
smsIntent.putExtra("address", number);
smsIntent.putExtra("sms_body", "");
startActivity(smsIntent);
listen();
} else if (matches.contains("open contacts")) {
Intent i = new Intent();
i.setComponent(new ComponentName("com.android.contacts", "com.android.contacts.DialtactsContactsEntryActivity"));
i.setAction("android.intent.action.MAIN");
i.addCategory("android.intent.category.LAUNCHER");
i.addCategory("android.intent.category.DEFAULT");
returnedText.setText("");
startActivity(i);
listen();
} else if (matches.contains("open music")) {
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
returnedText.setText("");
startActivity(intent);
listen();
} else if (matches.contains("set alarm")) {
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
if (intent.resolveActivity(getPackageManager()) != null) {
returnedText.setText("");
startActivity(intent);
}
listen();
} else if (matches.contains("open timer")) {
Intent intent = new Intent(AlarmClock.ACTION_SET_TIMER);
if (intent.resolveActivity(getPackageManager()) != null) {
returnedText.setText("");
startActivity(intent);
}
listen();
} else if (matches.contains("open calendar")) {
ComponentName cn;
Intent i = new Intent();
cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");
i.setComponent(cn);
returnedText.setText("");
startActivity(i);
speech.destroy();
} else if (matches.contains("what is date and time now")) {
returnedText.setText(DateFormat.getDateTimeInstance().format(new Date(0)));
speaking.speakWords(DateFormat.getDateTimeInstance().format(new Date(0)).toString());
listen();
} else if (matches.contains("what is date today")) {
returnedText.setText(DateFormat.getDateInstance().format(new Date(0)));
speaking.speakWords(DateFormat.getDateInstance().format(new Date(0)).toString());
listen();
} else if (matches.contains("what is time now")) {
returnedText.setText(DateFormat.getTimeInstance().format(new Date(0)));
speaking.speakWords(DateFormat.getTimeInstance().format(new Date(0)).toString());
listen();
} else if (matches.contains("close music")) {
if (getIntent().toString().contains(Intent.CATEGORY_APP_MUSIC)) {
finish();
listen();
}
}
}
public void texttospeech(final String text) {
tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.getDefault());
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
});
}
@Override
public void onRmsChanged(float rmsdB) {
Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
progressBar.setProgress((int) rmsdB);
}
public static String getErrorText(int errorCode) {
String message;
switch (errorCode) {
case SpeechRecognizer.ERROR_AUDIO:
message = "Audio recording error";
break;
case SpeechRecognizer.ERROR_CLIENT:
message = "Client side error";
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
message = "Insufficient permissions";
break;
case SpeechRecognizer.ERROR_NETWORK:
message = "Network error";
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
message = "Network timeout";
break;
case SpeechRecognizer.ERROR_NO_MATCH:
message = "No match";
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
message = "RecognitionService busy";
break;
case SpeechRecognizer.ERROR_SERVER:
message = "error from server";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
message = "No speech input";
break;
default:
message = "Didn't understand, please try again.";
break;
}
return message;
}