Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

android - Why is the ACTION_CHECK_TTS_DATA Intent "awkward to use"?

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:

  1. Create TextToSpeech
  2. 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"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...