我试图在文本到语音的开头和结尾调用一些方法,所以我使用了setOnUtteranceProgressListener,但是它无法工作/被调用。
我做错了什么?
这里需要的代码:
类别:
public class SpeechRecognizerActivity extends Activity implements TextToSpeech.OnInitListener
初始化方法:
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
String language = Locale.getDefault().getLanguage();
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
说话开始的方法:
private void speakOut(String text) {
setTtsListener();
tts.setPitch(1.5f);
tts.setSpeechRate(1.5f);
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
监听器(在speakOut中调用方法): 这些日志都不会显示在Logcat中。
private void setTtsListener() {
if (Build.VERSION.SDK_INT >= 15)
{
int listenerResult = tts.setOnUtteranceProgressListener(new UtteranceProgressListener()
{
@Override
public void onDone(String utteranceId)
{
Log.d(TAG,"progress on Done " + utteranceId);
}
@Override
public void onError(String utteranceId)
{
Log.d(TAG,"progress on Error " + utteranceId);
}
@Override
public void onStart(String utteranceId)
{
Log.d(TAG,"progress on Start " + utteranceId);
}
});
if (listenerResult != TextToSpeech.SUCCESS)
{
Log.e(TAG, "failed to add utterance progress listener");
}
}
else
{
int listenerResult = tts.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener()
{
@Override
public void onUtteranceCompleted(String utteranceId)
{
Log.d(TAG,"progress on Completed " + utteranceId);
}
});
if (listenerResult != TextToSpeech.SUCCESS)
{
Log.e(TAG, "failed to add utterance completed listener");
}
}
}
我找到了一个小小的解决方法:
boolean speakingEnd = tts.isSpeaking();
do {
speakingEnd = tts.isSpeaking();
} while ((speakingEnd));
Log.d(TAG,"talking stopped");
我已将它添加到speakOut方法的结尾,但此解决方案并不是很好。 与听众合作将是完美的。
那么我做错了什么?
答案 0 :(得分:31)
致电tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
时
您需要使用KEY_PARAM_UTTERANCE_ID
HashMap<String, String> map = new HashMap<String, String>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"messageID");
tts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
这让TextToSpeech知道如何使用你的话语听众text