我有一个支持多种语言的应用,它使用文字转语音在RecylerView
中说出信息。当选择用法语发言时,它不会在第一次点击时说话需要时间在Android 7.0和8.0中说话。下面是我的
语言适配器:
if (cnt.getCoun().equalsIgnoreCase("German")) {
Read.getInstance().setLanguage(Locale.GERMAN);
} else if (cnt.getCoun().equalsIgnoreCase("French")) {
Read.getInstance().setLanguage(Locale.FRENCH);
}
else if (cnt.getCoun().equalsIgnoreCase("Italian")) {
Read.getInstance().setLanguage(Locale.ITALIAN);
}
else if (cnt.getCoun().equalsIgnoreCase("Chinese")) {
Read.getInstance().setLanguage( new Locale("zh", "CN", ""));
}
else if (cnt.getCoun().equalsIgnoreCase("Japanese")) {
Read.getInstance().setLanguage(Locale.JAPANESE);
}
以下是我的申请文件
public class Read extends Application {
public TextToSpeech tts;
private static Read ourInstance;
public static Read getInstance() {
return ourInstance;
}
@Override
public void onCreate() {`enter code here`
super.onCreate();
ourInstance = this;
if (tts == null) {
tts = new TextToSpeech(getInstance(), status -> {
status = TextToSpeech.ERROR;
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.GERMAN);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(getApplicationContext(), "This language is not supported", Toast.LENGTH_SHORT).show();
}
}
});
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void listen(String message, Locale language) {
try {
if (tts != null) {
tts.setLanguage(language);
tts.setSpeechRate(.5f);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tts.speak(message, TextToSpeech.QUEUE_ADD, null, null);
} else {
tts.speak(message, TextToSpeech.QUEUE_ADD, null);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public int setLanguage(Locale language) {
textToSpeech.setLanguage(language);
}
public void stop() {
try {
if (textToSpeech != null)
{
textToSpeech.stop();
}
}catch(Exception e) {
e.printStackTrace();
}
}
其他语言没有问题