[英]How to call TextToSpeech activity from another activity?
I have an activity ( MainActivity
) that implements TextToSpeech
and works perfectly. 我有一个活动(
MainActivity
),它实现了TextToSpeech
并且可以完美地工作。 When a button's onClick
is called, it speaks whatever is typed in EditText
. 调用按钮的
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);
}
}
}
Now, I want to call MainActivity
from another activity and pass a string to speak up. 现在,我想从另一个活动中调用
MainActivity
并传递一个字符串说出来。 I tried: 我试过了:
MainActivity mainactivity = new MainActivity();
String word;
word = "speak";
mainactivity.speakText2(word); // Error
But, getting 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)
I tried using intent from another activity: 我尝试使用其他活动的意图:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("word", word);
startActivity(intent);
But, getting error: 但是,出现错误:
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}
I tried to implement TextToSpeech
in the activity I want to use it in. But, it does not work for the first time I call speakText2 and give error: 我试图在我想在其中使用的活动中实现
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}
For rest of the time it work perfectly. 在其余时间中,它运行良好。 Any idea how to fix this?
任何想法如何解决这个问题?
You can only let the engine speak, after onInit is done, so do following in onInit(): 在onInit完成后,您只能让引擎讲话,因此在onInit()中执行以下操作:
if (status == TextToSpeech.SUCCESS) { speakText2(word); 如果(状态== TextToSpeech.SUCCESS){talkText2(word);
} }
You have to use an Intent
to start an Activity
. 您必须使用
Intent
来启动Activity
。 See https://developer.android.com/training/basics/firstapp/starting-activity.html 参见https://developer.android.com/training/basics/firstapp/starting-activity.html
When you create your Activity
instance manually the onCreate
method is not called (and any other lifecycle methods) - that's why you're getting an NPE accessing your engine
property - it wasn't initialized. 当您手动创建
Activity
实例时,不会调用onCreate
方法(以及任何其他生命周期方法)-这就是为什么让NPE访问您的engine
属性的原因-尚未初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.