Why is the ACTION_CHECK_TTS_DATA Intent “awkward to use”?

前端 未结 1 1828
醉酒成梦
醉酒成梦 2020-12-06 14:57

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

相关标签:
1条回答
  • 2020-12-06 15:17

    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.

    0 讨论(0)
提交回复
热议问题