Android 中的 TTS 和 Singleton
我有一个应用程序大量使用 TTS。它工作正常,但我需要调整它。
我在每个屏幕中都使用 TTS 对象,但这并不好。我希望我可以只创建一次 TTS 对象(就像 Singleton),然后在我的所有活动中使用它。
以下是实现此功能的基本代码:
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();
}
}
让 TTS 对象在我的所有活动中可用的正确方法是什么?请记住,它使用一些方法,例如 startActivityForResult 等...所以...我想知道我可以做些什么来使这项工作正常进行。
有人可以帮我吗?
任何帮助表示赞赏!
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这相对简单 - 只需按如下方式实现即可,并且不要忘记作为上下文传递,应用程序上下文 (
this.getApplicationContext()
) 而不是活动上下文。}
This is relatively simple - Just implement it as follows and do not forget to pass as context, the application context (
this.getApplicationContext()
) and not the activity context.}
看看这里,在 android 中以更好的设计在活动和程序之间共享内容:
Intent.putExtras 大小限制?
Have a look here to share stuff between activities and program with nicer design in android :
Intent.putExtras size limit?
您可能希望在应用程序中实例化并保存 Singleton 对象,无论如何,该对象只能实例化一次,因此只需将 TTS 初始化代码放入其自己的类中,然后将该类实例化为应用程序中的对象即可。您需要将应用程序传递到 TTS 类,作为实例化 TTS 的上下文。
那么您可以在 Activity 中使用 getApplication().myTTS.whateverMethodsYouMake(yadayada) 来访问 Application-Singleton 化的 TTS 类。
You'll likely want to instantiate and hold your Singleton object in the Application, which can only be instantiated once anyway, so just put your TTS initialisation code in a class of its own and instantiate that class as an object in your Application. You'll need to pass the Application to your TTS class as the Context that TTS will be instantiated with.
then you can use getApplication().myTTS.whateverMethodsYouMake(yadayada) from within Activities to access the Application-Singleton-ified TTS class.