public static void googleSpeechEngine(Activity activity) {
googleSpeechEngine(activity, "");
}
public static void googleSpeechEngine(Activity activity, int res) {
googleSpeechEngine(activity, activity.getResources().getString(res));
}
public static void googleSpeechEngine(Activity activity, String prompt) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "fa-IR");
try {
activity.startActivityForResult(intent, Config.REQUEST_CODE_GOOGLE_SPEECH); //Config.REQUEST_CODE_GOOGLE_SPEECH = 200
} catch (ActivityNotFoundException a) {
G.toast(R.string.voice_search_not_support, true);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case Config.REQUEST_CODE_GOOGLE_SPEECH: { //Config.REQUEST_CODE_GOOGLE_SPEECH = 200
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
G.toast(result.get(0));
}
break;
}
}
}
MyUtils.googleSpeechEngine(ActivityBase.this, R.string.for_example_say_cheese);