如何从其他活动调用TextToSpeech活动?

时间:2017-11-12 15:25:04

标签: java android text-to-speech

我有一个实现MainActivity的活动(TextToSpeech)并且运作正常。当一个按钮的onClick被调用时,它会说出EditText中输入的内容。

MainActivity:

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{

    private TextToSpeech engine;
    private EditText text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (EditText) findViewById(R.id.text);
        engine = new TextToSpeech(this, this);

        Intent i=getIntent();
        Bundle b=i.getExtras();
        word=b.getString("word");
        speakText2(word);
    }

    // speakText is called by onClick button
    public void speakText(View v) {
        String textContents = text.getText().toString();
        engine.speak(textContents, TextToSpeech.QUEUE_FLUSH, null, null);
    }

    public void speakText2(String textContents) {
        engine.speak(textContents, TextToSpeech.QUEUE_ADD, null, null);
    }

    @Override
    public void onInit(int i) {
        if (i == TextToSpeech.SUCCESS) {
            //Setting speech Language
            engine.setLanguage(Locale.ENGLISH);
            engine.setPitch(1);
        }
    }
}

现在,我想从另一个活动中调用MainActivity并传递一个字符串来说出来。 我试过了:

MainActivity mainactivity = new MainActivity();
String word;
word = "speak";
mainactivity.speakText2(word); // Error

但是,得到错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.speech.tts.TextToSpeech.speak(java.lang.CharSequence, int, android.os.Bundle, java.lang.String)' on a null object reference
at MainActivity.speakText2(TTSEngine.java:53)

我尝试使用其他活动的意图:

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("word", word);
startActivity(intent);

但是,得到错误:

I/TextToSpeech: Sucessfully bound to com.google.android.tts
W/TextToSpeech: speak failed: not bound to TTS engine
W/TextToSpeech: speak failed: not bound to TTS engine
W/TextToSpeech: speak failed: not bound to TTS engine
I/TextToSpeech: Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
I/TextToSpeech: Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}

我尝试在我想要使用它的活动中实现TextToSpeech。但是,它在第​​一次调用speakText2并且给出错误时不起作用:

W/TextToSpeech: speak failed: not bound to TTS engine
I/TextToSpeech: Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
I/TextToSpeech: Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}

其余时间它完美运作。知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

在onInit完成后,你只能让引擎说话,所以在onInit()中执行以下操作:

if(status == TextToSpeech.SUCCESS){      speakText2(字);

}

答案 1 :(得分:0)

您必须使用Intent来启动Activity。见https://developer.android.com/training/basics/firstapp/starting-activity.html

当您手动创建Activity实例时,未调用onCreate方法(以及任何其他生命周期方法) - 这就是您获取NPE访问engine属性的原因 - 它没有初始化。