首页 > 解决方案 > 文字转语音停止说话,但循环暂停并没有停止

问题描述

我想在 tts 中添加暂停和播放功能。到目前为止所做的是,当我停止 tts 说话并想要得到最后一个单词但正在运行的循环没有停止时。请解决我的问题。

 public void toggleAudioMode() {
    isPlayerVisible = !isPlayerVisible;
    ivToggleAudioModeAnswer.setImageResource(isPlayerVisible ? R.drawable.ic_audio_off : R.drawable.ic_audio_on);
    if (isPlayerVisible) {
        splitspeech = result.split(" ");
        if (j > 0) {
            speakSpeech(j, "run");
        } else {
            speakSpeech(0, "run");
        }
    } else {
        keepGoing = false;
        speakSpeech(0, "stop");
    }
}

 public void speakSpeech(int m, String type) {
    myHash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "done");

    while (i < splitspeech.length) {
        if (type.equals("run")) {
         if (i == 0) { // Use for the first splited text to flush on audio stream
          textToSpeech.speak(splitspeech[i].trim(), QUEUE_FLUSH, myHash);
            } else { // add the new text on previous then play the TTS
          textToSpeech.speak(splitspeech[i].trim(), QUEUE_ADD, myHash);
                j = i;
            }
        } 
        i++;
    }
    if (type.equals("stop")) {
        textToSpeech.stop();
    }
}

我想要播放和暂停功能。

标签: androidtext-to-speech

解决方案


推荐阅读