我遇到了android TTS初始化延迟的常见问题,这个问题非常高。我正在使用Android Nougat为智能手表构建一个应用程序,我需要2个TTS服务,一个用一种语言说一个文本,另一个用第二语言说一个文本。事实证明,即使TextToSpeech
方法的Voice
方法,每次更改onInit
对象以更改语言时,OnInitListener
对象的同一实例内的语言之间的切换也会引入初始延迟。 {1}}已被调用。
我尝试通过创建具有不同语言的TextToSpeech
类的2个实例并在需要时调用相应的speak()
方法来修复此问题,但延迟仍然存在。我的申请是否有解决方法?
CODE:
public class TTSSpeaker implements TextToSpeech.OnInitListener{
//TTS object
private TextToSpeech tts;
//Locale
private Locale locale;
//Language code
private String code;
//Initialized flag
private boolean initialized;
//ID
private String utteranceId;
//Constructor
public TTSSpeaker(Context context, String languageCode, String id){
code = languageCode;
initialized = false;
utteranceId = id;
tts = new TextToSpeech(context, this);
}
@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS){
initialized = true;
Locale closest = Locale.forLanguageTag(code);
tts.setLanguage(closest);
Set<Voice> voices = tts.getVoices();
int lowestLatency = Voice.LATENCY_VERY_HIGH;
Voice selected = tts.getDefaultVoice();
for(Voice voice : voices){
if(voice.getLatency() <= lowestLatency
&& voice.getLocale().getLanguage().equals(code)
&& !voice.isNetworkConnectionRequired()){
lowestLatency = voice.getLatency();
selected = voice;
}
}
tts.setVoice(selected);
}
}
public void speakText(String text){
if(initialized) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
}
}
public boolean isInitialized(){return initialized;}
public void finishTTS(){
if(tts != null){
tts.stop();
tts.shutdown();
}
}
}
我如何使用这个课程:
private void speakRecording(){
if (ttsSpeakerOrigin.isInitialized()){
ttsSpeakerOrigin.speakText(recordedText);
}
else
Log.d("Debug", "Not Initialized");
}
private void speakTranslation(){
if (ttsSpeakerTranslation.isInitialized()){
ttsSpeakerTranslation.speakText(translation);
}
else
Log.d("Debug", "Not Initialized");
}
ttsSpeakerTranslation
和ttsSpeakerOrigin
是TTSSpeaker的实例,它们对应于使用不同语言的TTS服务。问题是,即使它们被初始化,也要在另一个引入谷歌与此服务相同的初始化延迟之后再说一次