I want to use TTS
in an Android
application. I followed introduction-to-text-to-speech-in. And this is the code of the Activity
which creates TTS
instance:
public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
private int MY_DATA_CHECK_CODE = 0;
private TextToSpeech myTTS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
myTTS = new TextToSpeech(this, this);
}
else {
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
@Override
public void onInit(int status) {
}
}
As you can see it is straightforward and simple and also works up to Android 8.1-API 27
; but in Android 9.0
I get ActivityNotFoundException
:
- in
onCreate
method:No Activity found to handle Intent { act=android.speech.tts.engine.CHECK_TTS_DATA }
- in
onActivityResult
method:No Activity found to handle Intent { act=android.speech.tts.engine.INSTALL_TTS_DATA }
Although with attention to documentation about ACTION_CHECK_TTS_DATA and ACTION_INSTALL_TTS_DATA, no one of them is deprecated. How I can solve above errors?