次のコードを使用してテキストを認識しています。最初にダイアログを呼び出して、認識用の言語を選択します。その引数を RecognizerIntent に渡します。残念ながら、私の電話では「en-US」しか認識されません(「fr-FR」と「ru-RU」も試しました)。まだ行っていないことはありますか、またはリストを作業言語に絞り込むにはどうすればよいですか?
コード
public void recognize(View v) {
Intent detailsIntent = new Intent(
RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
sendOrderedBroadcast(detailsIntent, null, new LanguageDetailsChecker(
context), null, Activity.RESULT_OK, null, null);
}
private class LanguageDetailsChecker extends BroadcastReceiver {
Context contextApp;
public LanguageDetailsChecker(Context context) {
this.contextApp = context;
}
@Override
public void onReceive(Context context, Intent intent) {
Bundle results = getResultExtras(true);
ArrayList<String> languages = new ArrayList<String>();
if (results.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE)) {
languagePreference = results
.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE);
}
if (results.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
languages = results
.getStringArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
}
supportedLanguages = new String[languages.size()];
supportedLanguages = languages.toArray(supportedLanguages);
AlertDialog.Builder b = new Builder(contextApp);
for (String s : supportedLanguages) {
Log.d("Supported languages", s);
}
b.setTitle("Choose your language");
b.setItems(supportedLanguages, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
chooseLanguage(which);
}
});
b.show();
}
private void chooseLanguage(int i) {
int which = i;
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
supportedLanguages[which]);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
supportedLanguages[which]);
intent.putExtra(
RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE,
supportedLanguages[which]);
Log.d("Languages choosen", supportedLanguages[which]);
startActivityForResult(intent, 300);
}
}