Intent (TextToSpeech.Engine.ACTION_CHECK_TTS_DATA) を使用して、TTS データがデバイスにインストールされているかどうかを確認しようとしていると思います。ちなみに、これはデフォルトのデバイス言語をチェックし、インストールされていない場合、resultCodeをTextToSpeech.Engine.ACTION_INSTALL_TTS_DATA onActivityResult() として返します。
TTS を初期化し、エラーを処理する適切な方法を以下に示します。
system = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
system.setRecognitionListener(this);
speech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
result = speech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not available, attempting download");
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
else {
Log.e("TTS", "Initialization Failed!");
}
}
}, "com.google.android.tts");
ここで 3 つの点に注意してください。
- Google Text To Speech を使用するためのパッケージ名は「com.google.android.tts」です。
- onInit()で処理されるインテント「ACTION_CHECK_TTS_DATA」をチェックする必要はありません。
- Text to Speech setlanguage はコストのかかる操作であり、UI スレッドをフリーズさせます。デフォルトの言語を使用する場合は、削除してください。
この方法を使用すると、ダイアログのポップアップが表示されず、tts が初期化されます。それが役立つかどうか教えてください!