Reputation: 183
I am using Text-To-Speech in my app. It works fine, when the voice data package is installed. However, when no voice data is installed and I call the following method, than no audio is played:
_textToSpeech.speak( text, TextToSpeech.QUEUE_ADD, null );
Therefore I want to check if the package is installed to give the user a notification, but I found no way to do this. The class "TextToSpeech" provides a method "isLanguageAvailable()". But if voice data is installed or not installed, the result of the method is always the same.
_textToSpeech.isLanguageAvailable(Locale.GERMANY) // result is 1 (LANG_COUNTRY_AVAILABLE)
_textToSpeech.isLanguageAvailable(Locale.GERMAN) // result is 0 (LANG_AVAILABLE)
In my android settings the preferred TTS engine is 'Google Text-to-speech'.
Has anyone a clue how to check if TTS voice data is installed?
Thanks..
Upvotes: 2
Views: 1932
Reputation: 940
I have discussed two approaches for how to check installed Google TTS voice data in your device below-
FlutterTts flutterTts = FlutterTts();
bool isInstalled = await LouieSdk.textToSpeech?.isLanguageInstalled("ta-IN");
if (!isInstalled){
// your relevant error feedback, install voice data first, and then continue.
}
(Here, "ta-IN" is the Tamil language locale; you can use your own language if you want.)
FlutterTts flutterTts = FlutterTts();
Map<String, String> installed = await flutterTts.areLanguagesInstalled(["ta-IN", "es-ES"]);
// Returns map like {"ta-IN": "1", "es-ES": "0"} where "1" means installed and "0" means not installed
These methods are available on flutter_tts package now, I don't exactly know in which version it got this support but this feature is supported now.
I hope I answered your question.
Upvotes: 0
Reputation: 367
Here is the approach which I used to check if the voice data is available or not.
private val ttsCheckCode = 1000
// Check by launching intent whether TTS is available or not
val checkIntent = Intent()
checkIntent.action = TextToSpeech.Engine.ACTION_CHECK_TTS_DATA
startActivityForResult(checkIntent, ttsCheckCode)
This will check whether the voice data is available or not. Check the documentation in android.speech.tts.TextToSpeech for more clarity. The above code will either give me CHECK_VOICE_DATA_PASS or CHECK_VOICE_DATA_FAIL. Once I receive CHECK_VOICE_DATA_PASS as result, I can do my operations like initialization, etc. in onActivityResult().
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == ttsCheckCode) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance and do other things
}
}
}
Hope this helps.
Upvotes: 0