Mat
Mat

Reputation: 2184

Android TTS - ACTION_CHECK_TTS_DATA default engine

In my app (Android API 10) I use the standard way to check if the TTS engine and voice are available:

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

In a device with more than a TTS engine Android asks to select one ("complete action using...")

Is there a way to check ACTION_CHECK_TTS_DATA using the default TTS engine (to avoid the engine selection dialog) ? Or ... how do I get the user selected TTS engine (to use it with setEngineByPackageName() (API 10)) ?

Upvotes: 2

Views: 1823

Answers (1)

brandall
brandall

Reputation: 6144

There are a few ways you can do this. To get the default TTS Engine, check in the onInit Listener:

TextToSpeech tts = new TextToSpeech(context, onInitListener);


    @Override
        public void onInit(final int status) {

            switch (status) {

            case TextToSpeech.SUCCESS:

                try {

                    final String initEngine = tts.getDefaultEngine();

    // Check it's != null before doing anything with it.

                } catch (final Exception e) {

                }

        break;
    }
}

The try/catch block is there as I've had crashes for some IVONA and SVOX TTS engines.

Once you have the package name, you can then query the intent activities and if necessary use a custom chooser or limit the intent to just the package name you got from above.

    private void getEngines(String initEngine) {

        Intent ttsIntent = new Intent();
        ttsIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);

        PackageManager pm = getPackageManager();

        List<ResolveInfo> list = pm.queryIntentActivities(ttsIntent,
                PackageManager.GET_META_DATA);

        for (ResolveInfo appInfo : list) {

// Might want a null check in here

            if(appInfo.activityInfo.applicationInfo.packageName.matches(initEngine) {

            ttsIntent
                    .setPackage(appInfo.activityInfo.applicationInfo.packageName);
        }


            startActivityForResult(ttsIntent, 33);

        }
    }

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 33 && data != null) {

       // do something
    }
}

I've not tested the above, but I'm sure you can build on it to get it to do what you want.

Upvotes: 2

Related Questions