I have an application using TTS very heavily. It's working fine, but I need to tweak it.
I am using a TTS object in every screen and this is not good. I wish I could creat the TTS object just once (like a Singleton) and them, use it throughout all my activities.
Here is the base code for this to work:
public class SimOuNaoActivity extends Activity implements OnInitListener{
public TextToSpeech tts;
private int MY_DATA_CHECK_CODE = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
tts.speak("Testing 1,2,3", TextToSpeech.QUEUE_ADD, null);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
tts = new TextToSpeech(this, this);
} else {
// missing data, install it
Intent installIntent = new Intent();
installIntent
.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
// Toast.LENGTH_LONG).show();
} else if (status == TextToSpeech.ERROR) {
// Toast.LENGTH_LONG).show();
}
}
@Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
System.gc();
}
}
What is the correct approach to have the TTS object available in all my activities? Have in mind that it uses some methods like startActivityForResult, etc... so... I would like to know what I can do to make this work fine.
Can anyone help me, please?
Any help is appreciatted!
Thanks!