我有一段代码可以合成语音。当前存在错误,我想找出哪个。
我在我的UtteranceProgressListener
对象上附加了一个TextToSpeech
。调用textToSpeech.speak
时,调用函数onError(String utteranceId)
,但不调用函数onError(String utteranceId, int errorCode)
。我已将APK版本设置为22,并使用了Sony Xperia XZ2 Compact。命令textToSpeech.getEngines()
返回com.google.android.tts
public class Text2Speech extends UtteranceProgressListener {
private Context mContext;
private TextToSpeech textToSpeech;
private final String SPEECH_ID = "SPEECH";
private boolean isReady = false;
public Text2Speech(Context context, final String src){
mContext = context;
System.out.println("text2Speech created");
textToSpeech = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
Log.e("Initialization: ", "Sucess");
isReady = true;
Locale locale = new Locale(src);
int ttsLang = textToSpeech.setLanguage(locale);
if (ttsLang == TextToSpeech.LANG_MISSING_DATA
|| ttsLang == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "The Language is not supported!");
} else {
Log.i("TTS", "Language Supported.");
}
Log.i("TTS", "Initialization success.");
} else {
Toast.makeText(mContext, "TTS Initialization failed!", Toast.LENGTH_SHORT).show();
}
}
});
textToSpeech.setOnUtteranceProgressListener(this); // <-- Assigning listener
}
public boolean isReady(){
return isReady;
}
public void checkSpeaking(){
if (textToSpeech.isSpeaking()){
textToSpeech.stop();
}
}
public void showMessage(String msg){
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
}
public void speakText(String text){
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, SPEECH_ID);
}
public void setLanguage(String src){
Locale locale = new Locale(src);
int tts = textToSpeech.setLanguage(locale);
System.out.println(tts + " " + src);
if (tts == TextToSpeech.LANG_MISSING_DATA
|| tts == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(mContext, "Language not yet supported.", Toast.LENGTH_LONG).show();
}
}
public void stop(){
textToSpeech.stop();
textToSpeech.shutdown();
}
@Override
public void onStart(String utteranceId) {
Log.e("START", "start speaking");
}
@Override
public void onDone(String utteranceId) {
Log.e("DONE", "done speaking");
}
@Override
public void onError(String utteranceID){
Log.e("Error", utteranceID); // <-- This is called
}
@Override
public void onError(String utteranceId, int errorCode) {
Log.e("Error", "Error speaking"); <-- This is never called
switch (errorCode){
case TextToSpeech.ERROR_INVALID_REQUEST:
showMessage("Invalid Request");
break;
case TextToSpeech.ERROR_NETWORK:
showMessage("Network Error");
break;
case TextToSpeech.ERROR_NETWORK_TIMEOUT:
showMessage("Network Timeout");
break;
case TextToSpeech.ERROR_NOT_INSTALLED_YET:
showMessage("Error Not Yet Downloaded");
break;
case TextToSpeech.ERROR_OUTPUT:
showMessage("Output Error");
break;
case TextToSpeech.ERROR_SERVICE:
showMessage("Error of TTS service");
break;
case TextToSpeech.ERROR_SYNTHESIS:
showMessage("Error synthesizing");
break;
case TextToSpeech.LANG_NOT_SUPPORTED:
showMessage("Language nor supported");
break;
}
}
}
有人对此有经验吗?这个问题与this question.
有关