在 21 以上的 api 级别未调用 UtteranceProgressListener
UtteranceProgressListener is not getting called in api level above 21
我想知道为什么我的 UtteranceProgressListener 没有被调用。
这是我尝试这样做的方法
private UtteranceProgressListener progressListener=new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
Log.d("modroid", "speech started");
}
@Override
public void onDone(String utteranceId) {
Log.d("modroid", "done");
}
@Override
public void onError(String utteranceId) {
Log.d("modroid", "error");
}
};
private void prepareTxtToSpeech() {
textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
if (textToSpeech.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE) {
textToSpeech.setLanguage(Locale.ENGLISH);
textToSpeech.setSpeechRate(0.8f);
textToSpeech.setOnUtteranceProgressListener(progressListener);
}
} else if (status == TextToSpeech.ERROR) {
Toast.makeText(context, context.getString(R.string.txttospeech_error), Toast.LENGTH_LONG).show();
}
}
});
}
下面是我调用的说话方式
public void speak(String text) {
if (textToSpeech != null) {
Log.d("modroid", "speaking: ");
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, "some id");
}
}
我知道以前有人问过这类问题,但没有任何帮助。请让我知道我哪里做错了。
如果语言可用并且设置正确,您必须小心'validate':
switch (textToSpeech.isLanguageAvailable(Locale.US)){
case TextToSpeech.LANG_AVAILABLE:
break;
case TextToSpeech.LANG_COUNTRY_AVAILABLE:
break;
case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
break;
case TextToSpeech.LANG_MISSING_DATA:
break;
case TextToSpeech.LANG_NOT_SUPPORTED:
break;
}
您也应该以同样的方式检查 textToSpeech.setLanguage(Locale.ENGLISH)
的响应。
您可能会接受前三名之一。否则,您将需要处理问题。
我想知道为什么我的 UtteranceProgressListener 没有被调用。 这是我尝试这样做的方法
private UtteranceProgressListener progressListener=new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
Log.d("modroid", "speech started");
}
@Override
public void onDone(String utteranceId) {
Log.d("modroid", "done");
}
@Override
public void onError(String utteranceId) {
Log.d("modroid", "error");
}
};
private void prepareTxtToSpeech() {
textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
if (textToSpeech.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE) {
textToSpeech.setLanguage(Locale.ENGLISH);
textToSpeech.setSpeechRate(0.8f);
textToSpeech.setOnUtteranceProgressListener(progressListener);
}
} else if (status == TextToSpeech.ERROR) {
Toast.makeText(context, context.getString(R.string.txttospeech_error), Toast.LENGTH_LONG).show();
}
}
});
}
下面是我调用的说话方式
public void speak(String text) {
if (textToSpeech != null) {
Log.d("modroid", "speaking: ");
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, "some id");
}
}
我知道以前有人问过这类问题,但没有任何帮助。请让我知道我哪里做错了。
如果语言可用并且设置正确,您必须小心'validate':
switch (textToSpeech.isLanguageAvailable(Locale.US)){
case TextToSpeech.LANG_AVAILABLE:
break;
case TextToSpeech.LANG_COUNTRY_AVAILABLE:
break;
case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
break;
case TextToSpeech.LANG_MISSING_DATA:
break;
case TextToSpeech.LANG_NOT_SUPPORTED:
break;
}
您也应该以同样的方式检查 textToSpeech.setLanguage(Locale.ENGLISH)
的响应。
您可能会接受前三名之一。否则,您将需要处理问题。