Call TextToSpeech Activity Without Any UI Display

二次信任 提交于 2019-12-24 02:42:52

问题


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:


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)



来源:https://stackoverflow.com/questions/4771562/call-texttospeech-activity-without-any-ui-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!