The official introduction to Text-To-Speech in Android says that "upon creating your activity, a good first step is to check for the presence of the TTS resources with the corresponding intent:"
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
But @gregm in this thread suggests otherwise:
Also, don't use the ACTION_CHECK_TTS_DATA Intent, that's awkward to use.
Instead, do the following:
- Create TextToSpeech
- OnInit, check isLanguageAvailable() if it is, your app is all set. if not, send the ACTION_INSTALL_TTS_DATA
If I understand correctly, what @gregm does/suggests is to defer the TextToSpeech.LANG_MISSING_DATA
check from onActivityResult()
to onInit()
.
Why is this better than the formal approach?
And why is ACTION_CHECK_TTS_DATA so "awkward to use"?
This is @gregm.
ACTION_CHECK_TTS_DATA
requires more complicated execution and more code, but yet does the same thing as TextToSpeech.isLanguageAvailable()
Let me explain:
1) You need TWO asynchronous processes when you use ACTION_INSTALL_TTS_DATA
. First, to launch an Intent
and receive the result. Second, to wait for TextToSpeech
to call onInit()
More can go wrong while your app is doing all that waiting.
2) All you are trying to do is execute an if statement and you need the same code to handle installing the language data if need be, so why bother adding extra complexity?
It boils down do this:
Do you want 1 line of code:
if (TextToSpeech.isLanguageAvailable())
{
(same tts init code here)
}
or >1 lines of code like:
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
public void onActivityResult(...)
{
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
(same tts init code here)
}
In my point of view, I'd rather just get to the if statement, instead of using an Intent
runaround. I believe the official Android documentation needs to change to recommend this approach.
If you still don't believe that both approaches we are discussing do the same exact thing, check out the alternative implementations I have here and here. Also, If you don't care about all this stuff and just want your app to speak, just extend this Activity and be done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With