开发者

Call TextToSpeech Activity Without Any UI Display

开发者 https://www.devze.com 2023-02-06 18:39 出处:网络
What is the recommended way to call TextToSpeech without inv开发者_Go百科oking any UI change? The examples given are all bound to Activities, and the default behavior for an activity is to display its

What is the recommended way to call TextToSpeech without inv开发者_Go百科oking 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#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)

0

精彩评论

暂无评论...
验证码 换一张
取 消