在 Android 文本转语音应用程序的当前活动中朗读文本后调用活动

发布于 2024-12-03 03:18:07 字数 1066 浏览 1 评论 0原文

我是安卓新手。任何人都可以帮助我满足以下要求吗?在当前活动的演讲完成后调用活动。

Activity2 的文本在 Activity1 完成后读取,但 Activity2 在 Activity1 文本完成之前启动。在此话语中,id 也未被识别。

我写的代码如下。任何帮助将不胜感激。谢谢。

 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);
    }
 }   

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 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);
    }
 }   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后咖啡店 2024-12-10 03:18:07

您的代码顺序错误:您需要在调用 speak 之前将 put 放入 HashMap 中。试试这个:

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文