how to get to know programmatically whether any TTS engine installed in my device or not?

前端 未结 4 685
逝去的感伤
逝去的感伤 2021-01-05 01:25

I would like to know programmatically how to get TTS engine info of the device for e.g. whether any TTS engine is installed or not , if installed then what are those and wha

相关标签:
4条回答
  • 2021-01-05 01:58

    This official Android Blog Post gives you the best practice to detect if the TTS Engine is installed and ready to use, as well as other practices on TTS.

    0 讨论(0)
  • 2021-01-05 02:06

    To save your clicks:

    Launch this to check if TTS is installed or not:

    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    

    and then get result in this:

    private TextToSpeech mTts;
    protected void onActivityResult(
        int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // success, create the TTS instance
            mTts = new TextToSpeech(this, this);
        } else {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(
                TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }
    }
    
    0 讨论(0)
  • 2021-01-05 02:12

    You can check it by first sending an intent for result

    Intent intent = new Intent();
    intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(intent, 0);
    

    Then you can check it that if you have installed TTS engine or not in onActivityResult method:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == 0){
        if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
        Toast.makeText(getApplicationContext(),"Already Installed", Toast.LENGTH_LONG).show();
    } else {
        Intent installIntent = new Intent();
        installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
        startActivity(installIntent);
        Toast.makeText(getApplicationContext(),"Installed Now", Toast.LENGTH_LONG).show();
    }
    

    Hope it works :)

    0 讨论(0)
  • 2021-01-05 02:12

    This gives you the list of TTS Engines installed on your Android.

    tts = new TextToSpeech(this, this);
    for (TextToSpeech.EngineInfo engines : tts.getEngines()) {
    Log.d("Engine Info " , engines.toString());
    }
    
    0 讨论(0)
提交回复
热议问题