Android:等到tts说完,然后继续

时间:2011-01-25 02:39:39

标签: android

我真的很挣钱......我有几句我想读的句子,无论是通过tts speek功能还是通过屏幕上的文字,一次一个句子。

我已准备好textview区域,但将它放在一起就是我没有得到的。要么它会读取所有的句子,只显示最后一个句子,要么只显示和读取第一个句子。

任何人都知道我如何才能实现这一目标?

4 个答案:

答案 0 :(得分:4)

我刚刚遇到此问题,根据speak method,请使用UtteranceProgressListener。我发现这不是在UI线程上执行的,所以我不得不使用runOnUiThread()来返回更新活动。

tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
        @Override
        public void onStart(String utteranceId) {

        }

        @Override
        public void onDone(String utteranceId) {
            LettersActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // Do something on UI thread
                }
            });
        }

        @Override
        public void onError(String utteranceId) {
            Log.e(TAG, "error on " + utteranceId);
        }
    });

答案 1 :(得分:1)

public void speak(String message){        
    tts.speak(message, TextToSpeech.QUEUE_FLUSH, null); 
    while (tts.isSpeaking()){
        System.Out.Println("Do something or nothing while speaking..")
    } 
}

答案 2 :(得分:0)

boolean speakingEnd = tts.isSpeaking();
do{
   speakingEnd = tts.isSpeaking();
} while (speakingEnd);
//Continue with code

答案 3 :(得分:0)

试试这个

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{


    private boolean initialized;
    private String queuedText;
    private String TAG = "TTS";
    private TextToSpeech tts;




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



        tts = new TextToSpeech(this /* context */, this /* listener */);
        tts.setOnUtteranceProgressListener(mProgressListener);


        speak("hello world");

    }




    public void speak(String text) {

        if (!initialized) {
            queuedText = text;
            return;
        }
        queuedText = null;

        setTtsListener(); // no longer creates a new UtteranceProgressListener each time
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
        tts.speak(text, TextToSpeech.QUEUE_ADD, map);
    }


    private void setTtsListener() {

    }





    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            initialized = true;
            tts.setLanguage(Locale.ENGLISH);

            if (queuedText != null) {
                speak(queuedText);
            }
        }
    }



    private abstract class runnable implements Runnable {
    }




    private UtteranceProgressListener mProgressListener = new UtteranceProgressListener() {
        @Override
        public void onStart(String utteranceId) {
        } // Do nothing

        @Override
        public void onError(String utteranceId) {
        } // Do nothing.

        @Override
        public void onDone(String utteranceId) {

            new Thread()
            {
                public void run()
                {
                    MainActivity.this.runOnUiThread(new runnable()
                    {
                        public void run()
                        {

                            Toast.makeText(getBaseContext(), "TTS Completed", Toast.LENGTH_SHORT).show();

                        }
                    });
                }
            }.start();

        }
    };


}