Question

I have a code to check the TTS engine availability in the device. As the statement tts.getEngines().size() < 1 will be executed only from the API 14. I need to know if there is any way(methods) to check the same functionality even from the API 8.

Thanks in Advance

Code:

private void checkTTSAvailability() {
    tts = new TextToSpeech(this, this);
    ttsPresent=true;
    **if (tts.getEngines().size() < 1) {** 
        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) {
            Log.i("DragDrop", "No voice support on your phone"); 
            Dialog d = new Dialog(this);
            d.setTitle("Alert  ");
            TextView tv = new TextView(this);
            tv.setText("No voice support on your phone");
            d.setContentView(tv);
            d.show();
            ttsPresent=false;  
            // Not able to find the activity which should be started for
            // this intent
        } else {
            Dialog d = new Dialog(this);
            d.setTitle("Alert  ");
            TextView tv = new TextView(this);
            tv.setText("Installing");
            d.setContentView(tv);
            d.show();
            Log.i("DragDrop", "Installing");
            startActivity(installIntent);
        }
    }
}
Was it helpful?

Solution

AFAIK, there is no way to list TTS engines or otherwise know they are installed, below API 14. Maybe you could review a list of all installed packages and recognize known TTS engine names there, but it's far from ideal.

The correct way of doing this would be to implement TextToSpeech.OnInitListener() callback in your activity, to know when the default system TTS engine was initialized, and if the initialization was a success or failure. Then, from within this callback on SUCCESS return, first set the language you need on the TTS object, then send an Intent to TTS to check for installed data (ACTION_CHECK_TTS_DATA) for that language, and again when the activity result comes, call ACTION_INSTALL_TTS_DATA if needed.

For a code example, see my other post, link below. It compares async operations in Android Java and C# for Android (with Xamarin for Android), but the Java code sample given there has the correctly functioning OnInitListener() callback.

How to implement Android callbacks in C# using async/await with Xamarin or Dot42?

Greg

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top