private void initTTS() {
disableSpeakButton();
Intent checkIntent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, TTS_DATA_CHECK);
}
/**
* Callback from check for text to speech engine installed
* If positive, then creates a new <code>TextToSpeech</code> instance which will be called when user clicks on the 'Speak' button
* If negative, creates an intent to install a <code>TextToSpeech</code> engine
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TTS_DATA_CHECK) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
tts = new TextToSpeech(this, new OnInitListener() {
public void onInit(int status) {
if ( (status == TextToSpeech.SUCCESS) && (tts.isLanguageAvailable(Locale.US) >= 0) ) {
tts.setLanguage(Locale.US);
}
enableSpeakButton();
}
});
} else {
PackageManager pm = getPackageManager();
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
ResolveInfo resolveInfo = pm.resolveActivity( installIntent, PackageManager.MATCH_DEFAULT_ONLY );
if( resolveInfo == null ) {
Toast.makeText(TTSWithIntent.this,
"There is no TTS installed, please download it from Google Play",
Toast.LENGTH_LONG).show();
} else {
startActivity( installIntent );
}
}
}
}