Android enables an application to query the platform for the availability of language files: simply instantiate the intent below and send it in an asynchronous request by using startActivityForResult
method.
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, TTS_CHECK_DATA_REQUEST_CODE);
The result of the above request is returned by calling the onActivityResult
method: the second argument is a value which represents the success or failure of the above request, and the third argument contains some additional data. Among the additional data, there are two lists (possibly empty):
the available voices;
ArrayList<String> availableVoices = data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES); Log.i(LOG_TAG, String.format("Available voices: %s %s", availableVoices.size(), availableVoices.toString()));
the unavailable voices.
ArrayList<String> unavailableVoices = data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES); Log.i(LOG_TAG, String.format("Unavailable voices: %s %s", unavailableVoices.size(), unavailableVoices.toString()));
In the case of a simple request like the one shown above, the list of unavailable voices is empty, while the array of available voices is filled with all the voices installed on the system.
The behavior changes if you add the TextToSpeech.Engine.EXTRA_CHECK_VOICE_DATA_FOR
extra: according to the documentation, this extra is used to give instructions to the TTS engine that checks for some specific voices. Here is an exsample of source code:
ArrayList<String> checkVoiceDataFor = new ArrayList<String>(3);
checkVoiceDataFor.add("ita-ITA"); // installed on emulator
checkVoiceDataFor.add("eng-USA-female"); // not installed
checkVoiceDataFor.add("eng-USA"); // installed on emulator
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
checkIntent.putStringArrayListExtra(TextToSpeech.Engine.EXTRA_CHECK_VOICE_DATA_FOR, checkVoiceDataFor);
I expect that the response contains:
- ita-ITA and eng-USA in the list of available voices;
- eng-USA-female in the list of unavailable voices.
However, the results are as follows:
- Available voices: 2 [eng-USA ita-ITA]
- Unavailable voices: 0 []
Why?