What is the recommended way to call TextToSpeech without invoking any UI change? The examples given are all bound to Activities, and the default behavior for an activity is to display its own UI.
I'm trying to call a TextToSpeechActivity via my main activity via an Intent. I don't want the UI to change at all. I want the TextToSpeech to sound without anything in the UI changing. Here's what I have so far.
public class MyActivity extends Activity {
public void onClick(View v) {
Intent intent = new Intent(this, TextToSpeechActivity.class);
startActivity(intent);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Every time I click, the main UI is replaced with the UI for the TextToSpeech activity. And no, I don't want the main Activity to implement TextToSpeech.OnInitListener. There's already enough code in main. It's messy enough already.
You don't need to start a new activity. The hairy thing with TTS is that you need to have some initialization done before you can use it. And doing that e.g. within
onClick()
does not work at all. I've implemented that in Zwitscher: https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/OneTweetActivity.java#L317speak()
is called from a button on the UI, and the initialization needed is called from withinonCreate()
: https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/OneTweetActivity.java#L62 And don't forget to shut down the TTS system if no longer needed: https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/OneTweetActivity.java#L394HTH ( and let me know if there is a better solution)