开发者

To call an Activity after the text has spoken in the current activity in Android text to speech app

开发者 https://www.devze.com 2023-04-01 18:24 出处:网络
I am new to android. Can any one help me with the following requirement; to call an activity after the speech of the current activity has completed.

I am new to android. Can any one help me with the following requirement; to call an activity after the speech of the current activity has completed.

The开发者_StackOverflow社区 text of activity2 is read after completion of activity1 but activity2 is launched before activity1 text is completed. In this utterance id is also not identified.

The code I have written is follows. Any help would be appreciated. Thanks.

 public void onInit(int status) {       
    if (status == TextToSpeech.SUCCESS) {

        String text= tt.getText().toString();
        if (text!=null && text.length()>0) {
     Toast.makeText(activity1.this, "Speaking: " + text, Toast.LENGTH_LONG).show();

     tts.setOnUtteranceCompletedListener(this);
     HashMap<String, String> myHash = new HashMap();

     tts.speak(text, TextToSpeech.QUEUE_ADD, myHash);
     myHash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
    "completed");

     } 
    else if (status == TextToSpeech.ERROR) {
        Toast.makeText(Fossils.this,
                "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
    }

    }

}
public void onUtteranceCompleted(String t) {

    if(t == "completed"){
  Intent i = new Intent(this, activity2.class);
    startActivity(i);
    }
 }   


You have the wrong order in your code: you need to put into the HashMap before calling speak. Try this:

tts.setOnUtteranceCompletedListener(this);
HashMap<String, String> myHash = new HashMap();
myHash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "completed");
tts.speak(text, TextToSpeech.QUEUE_ADD, myHash);
0

精彩评论

暂无评论...
验证码 换一张
取 消