我正在使用 OnUtteranceCompletedListener 来检测 TTS 何时结束讲话,但是永远不会调用 OnUttereanceCompleted。监听器是在 onInit 中创建的,在 HashMap 之前,所以我不知道出了什么问题。任何想法?
public class TTSActivity extends Activity implements TextToSpeech.OnInitListener, OnUtteranceCompletedListener
{
private TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tts_layout);
tts = new TextToSpeech(this, this);
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status)
{
if (status == TextToSpeech.SUCCESS)
{
tts.setOnUtteranceCompletedListener(this);
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 {
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut()
{
HashMap<String, String> myHashAlarm = new HashMap<String, String>();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "FINISHED PLAYING");
tts.speak("Some text.", TextToSpeech.QUEUE_FLUSH, myHashAlarm);
}
@Override
public void onUtteranceCompleted(String utteranceId)
{
if(utteranceId=="FINISHED PLAYING")
{
Log.i("TTS", "Called"); //never called
}
}