Reputation: 52
I am trying to do a TTS app for Arabic however whenever I want to initialize my TTS it always fails. I have no idea what I'm supposed to do to fix it. I tried to enter the TTS engine on the emulator but it doesn't show me anything. So I'm unsure whether the issue is from the emulator or my code.
TTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status==TextToSpeech.SUCCESS){
int res = TTS.setLanguage(Locale.forLanguageTag("ar-XA"));
if (res == TextToSpeech.LANG_MISSING_DATA || res == TextToSpeech.LANG_NOT_SUPPORTED){
Log.e("TTS","Language not Supporter");
}
else{
speak.setEnabled(true);
}
}
else{
Log.e("TTS","Init Failed");
}
}
});
Upvotes: 3
Views: 5241
Reputation:
This is some information that may help you get TTS working on an android emulator.
When I first tried your code I got the LANG_NOT_SUPPORTED
error but soon realized my particular emulator instance did not have an TTS engine installed.
To install a TTS engine with an emulator I then had to use an emulator with Google Play (only a small subset of the AVD emulators have them). From the Android Virtual Device Manager you can Create Virtual Device
and see (under phone for example) a few with Play Store: Pixel 4, Pixel 3a, Pixel 3, Pixel, Nexus 5X, Nexus 5.
I then used the Nexus 5 with API 25. (I already had it created.)
With the the emulator running I can then see the Play Store
app listed. Run the Play Store app as you would on a phone (you'll have to authenticate yourself with your google account just as you would on a phone). Use the search bar and search for "speech services by google" - and install this app if not already installed. This is one TTS engine example which worked for me. Since this is the only TTS engine installed it is also the default. So if you choose to load other engines then you would have select the proper engine. (The engine name is a package name such as com.google.android.tts
.)
I then ran your code (slightly modified) as in the following. I added some diagnostics to list the available languages and variants. I also reviewed the available languages and regions here: https://cloud.google.com/speech-to-text/docs/languages and for example just chose "ar-iq" (but "ar" also worked).
Note your "ar-XA" is a language tag used for voices and not for the engine language.
TTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status==TextToSpeech.SUCCESS){
Set<Locale> avail = TTS.getAvailableLanguages();
for (Locale locale : avail) {
Log.e(TAG,"local: "+locale);
if (locale.getDisplayVariant() != null) {
Log.e(TAG," var: "+locale.getVariant());
}
}
List<TextToSpeech.EngineInfo> engineInfo = TTS.getEngines();
for (TextToSpeech.EngineInfo info : engineInfo) {
Log.e(TAG,"info: "+info);
}
int res = TTS.setLanguage(Locale.forLanguageTag("ar-iq"));
if (res == TextToSpeech.LANG_MISSING_DATA || res == TextToSpeech.LANG_NOT_SUPPORTED){
Log.e("TTS","Language not Supporter");
}
else{
//speak.setEnabled(true);
TTS.speak("اَلْعَرَبِيَّةُ", TextToSpeech.QUEUE_ADD, null);
}
}
else{
Log.e("TTS","Init Failed");
}
}
});
Once you have the TTS engine installed and your code or above code works - I think you can then use Settings
to install your voice data:
Settings | Speech | Text-to-speech output | (choose Speech Services by Google) and select the gear icon | and select your voice data.
You could also use the voice data programmatically.
Note you can install another TTS engine (not the default) by using the constructor:
TextToSpeech(Context context, TextToSpeech.OnInitListener listener, String engine)
The Arabic voices which come with the Google TTS engine have the following language tags - I'm guessing that when the language is set a preferred voice is used which is one of these:
ar-xa-x-arz-local
ar-xa-x-arc-local
Upvotes: 4