3

我需要 TextToSpeech 引擎一个接一个地说出我的话,我试图抓住一个单词的结束来开始说下一个单词。但OnUtteranceCompletedListener造成发言有些延迟。所以我的问题是,我该如何解决这个问题或更好地实施 OnUtteranceCompletedListener

public class AndroidTextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {

    int result = 0, CURRENT_WORD = 0;
    HashMap<String, String> myHash;
    String[] words;
    Button btnSpeak;
    TextToSpeech tts;
    Handler hand = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = "Hi there how are you";
        words = text.split(" ", 50);
        myHash = new HashMap<String, String>();
        tts = new TextToSpeech(this, this);

        btnSpeak = (Button) findViewById(R.id.btnSpeak);
        btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                hand.postDelayed(run, 300);
            }
        });
    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            result = tts.setLanguage(Locale.getDefault());
            tts.setPitch(1f);
            tts.setSpeechRate(1f);
        } else
            Log.e("TTS", "Init failed");
    }

    Runnable run = new Runnable() {
        public void run() {
            text = words[CURRENT_WORD];
            tts.speak(text, 1, myHash);
            tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {

                @Override
                public void onUtteranceCompleted(String utteranceId) {
                    CURRENT_WORD++;
                    hand.post(run1);
                }
            }); 
        }
    };

}
4

2 回答 2

2

您可以通过不在每次运行中重新创建 OnUtteranceCompleteListener 来加速它

OnUtteranceCompletedListener listener=new OnUtteranceCompletedListener(){
    @Override
    public void onUtteranceCompleted(String utteranceId) {
        CURRENT_WORD++;
        hand.post(run1);
    }
}
tts.setOnUtteranceCompletedListener(listener);
Runnable run = new Runnable() {
    public void run() {
        text = words[CURRENT_WORD];
        tts.speak(text, 1, myHash);
    }
};

此外,您可以使用 onUtteranceCompleted 方法调用 speak() 方法,而不是使用 Runnable 通过处理程序调用引擎的 speek() 方法

OnUtteranceCompletedListener listener=new OnUtteranceCompletedListener(){
    @Override
    public void onUtteranceCompleted(String utteranceId) {
        CURRENT_WORD++;
        if(CURRENT_WORD<max_words){
            String text=words[CURRENT_WORD];
            tts.speak(text,1,myHash);
        }
    }
}
tts.setOnUtteranceCompletedListener(listener);
Runnable run = new Runnable() {
    public void run() {
        text = words[CURRENT_WORD];
        tts.speak(text, 1, myHash);
    }
};
于 2014-03-20T09:51:23.287 回答
0

Locale.getDefault()如果不支持语言或需要数据文件,您的代码将失败。此外,如果onInit()在您按下 300 毫秒后仍未返回btnSpeakspeak()则不会起作用。您应该在 xml 布局文件中禁用btnSpeak并在onInit. 在btnSpeak listener循环中words调用speak()

btnSpeak = (Button) findViewById(R.id.btnSpeak);
    btnSpeak.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            for (int i = 0; i < words.length; i++) {
                tts.speak(text, 1, myHash);
                // call playSilence (long durationInMs, 1, myHash) 
                // if you want a slight delay between each word.
            }
        }
    });
于 2014-03-19T03:40:01.453 回答