Call TextToSpeech Activity Without Any UI Display

2019-07-20 12:51发布

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.

1条回答
Ridiculous、
2楼-- · 2019-07-20 13:33

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#L317 speak() is called from a button on the UI, and the initialization needed is called from within onCreate(): 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#L394

HTH ( and let me know if there is a better solution)

查看更多
登录 后发表回答