[英]TextToSpeech setLanguage not working?
I am setting my TextToSpeech to use a particular language (English - UK), using the locale "en_GB". 我正在设置我的TextToSpeech使用特定语言(英语 - 英国),使用语言环境“en_GB”。 But it always uses my devices default language. 但它总是使用我的设备默认语言。 Is there no way to set it programmatically? 有没有办法以编程方式设置它? I have downloaded the files required for the language and when I change my TTS's default language to 'English - UK' it works but when the default is different the programmatic approach does not work. 我已经下载了该语言所需的文件,当我将TTS的默认语言更改为“英语 - 英国”时,它可以正常工作,但是当默认语言不同时,程序化方法不起作用。 I have scoured the web to my best but am unable to resolve this issue. 我已尽力浏览网页,但无法解决此问题。
String ttsEngine = "com.google.android.tts";
txt2Speech = new TextToSpeech(this, this, ttsEngine);
//Locale ttsLocale = new Locale("eng", "GBR");
txt2Speech.setLanguage(new Locale("en_GB"));
Tried several methods, but none are working. 尝试了几种方法,但都没有。 Can I not set my TTS's language programmatically? 我不能以编程方式设置我的TTS语言吗?
Thank You 谢谢
EDIT: In response to 'A Honey Bustard' 编辑:回应'A Honey Bustard'
Other Code: 其他代码:
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener
My onInit()
我的onInit()
public void onInit(int status) {
// TODO Auto-generated method stub
}
Also I'm calling .setLanguage()
in my onCreate()
, as soon as my TextToSpeech
is initialized. 我的TextToSpeech
初始化后,我也在我的onCreate()
调用.setLanguage()
。 Is that correct? 那是对的吗? Also I'm only calling it once. 我也只打电话一次。 It is not required to call it every time right? 每次都不需要打电话吗? Also I'm testing on a GS7 我也正在测试GS7
Try the second Constructor from Locale that takes two Strings like this : 尝试使用Locale中的第二个构造函数,它接受两个字符串,如下所示:
txt2Speech.setLanguage(new Locale("en", "GB"));
EDIT : 编辑:
Yes it is usually ok to do instantiate it in onCreate, and it usually only needs and should be done once. 是的,通常可以在onCreate中实例化它,它通常只需要并且应该执行一次。
All I can do is show you my working code, I am setting the default language after instantiating in onCreate() : 我所能做的只是向您显示我的工作代码,我在onCreate()实例化后设置默认语言:
textToSpeech = new TextToSpeech(getApplicationContext(), this);
textToSpeech.setLanguage(Locale.getDefault());
In my app there are buttons in which you can change the language, which trigger this code (case British English) : 在我的应用程序中有一些按钮,您可以在其中更改语言,触发此代码(案例英国英语):
textToSpeech.setLanguage(new Locale("en", "GB"));
Maybe it is not available somehow , there are some checks you can validate if the language and country is available. 也许它不能以某种方式提供,如果语言和国家/地区可用,您可以验证一些检查。 You might find your error there. 你可能会在那里找到你的错误。
if (textToSpeech.isLanguageAvailable(new Locale("en", "GB")) == TextToSpeech.LANG_COUNTRY_AVAILABLE
&& textToSpeech.isLanguageAvailable(new Locale("en", "GB")) == TextToSpeech.LANG_AVAILABLE)
should return true. 应该回归真实。
You need to set the language once the Text to Speech Engine has initialised correctly. 一旦Text to Speech Engine正确初始化,您需要设置语言。
public void onInit(int status) {
switch (status) {
case SUCCESS:
// Set the language here
break;
case ERROR:
// Something went wrong. You can't set the language
break;
}
}
That should do it. 应该这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.