我已经多次问过这个问题但是他们中的任何一个实际上都没有帮助。当我启动tts时,会调用UtteranceProgressListener的onStart,但永远不会调用onEnd。
TTSClass.java
package com.michaelhoots.ttsapp;
import android.content.*;
import android.media.*;
import android.speech.tts.*;
import android.widget.*;
import java.util.*;
public class TTSClass {
private Context ctx = null;
private TextToSpeech tts = null;
private TextToSpeech.OnInitListener onInitListener = null;
private UtteranceProgressListener utteranceProgressListener = null;
private String toSpeak = "";
// handle start/stop
private void handleStart() {
HashMap<String, String> map = new HashMap<String, String>();
map.put(TextToSpeech.Engine.KEY_PARAM_SESSION_ID, "TTS_ID");
tts.setLanguage(Locale.US);
tts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, map);
}
private void handleStop() {
tts.stop();
tts.shutdown();
tts = null;
toSpeak = "";
}
// speak
public void speak(final String str) {
toSpeak = str;
onInitListener = new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
handleStart();
}
}
};
tts = new TextToSpeech(ctx.getApplicationContext(), onInitListener);
tts.setOnUtteranceProgressListener(utteranceProgressListener);
}
// constructor
public TTSClass(Context context) {
ctx = context;
utteranceProgressListener = new UtteranceProgressListener() {
@Override
public void onStart(String id) {
Toast.makeText(ctx, "Speech Started", Toast.LENGTH_SHORT).show();
}
@Override
public void onDone(String id) {
handleStop();
Toast.makeText(ctx, "Speech Finished", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String id) {}
};
}
}
当我打电话
speak("Text To Speech");
调用“onStart”方法并显示toast。但是,永远不会调用“onDone”方法,也不会显示其toast。我在这里做错了什么?