在我的 Android 应用程序中的多个不同活动中重用 TTS 对象
我正在为盲人编写一个 Android 应用程序。因此,我在应用程序中的每个 Activity 上都使用 TTS。
我不想再次在每个活动上初始化 TTS,因为这需要时间和资源,因此我在应用程序类中创建一个 TTS 对象并在活动上使用它。效果很好。
但文档说你必须这样做:
protected void onDestroy() {
super.onDestroy();
if (mTts != null) {
mTts.shutdown();
}
}
但我不能这样做,因为我不想关闭我的共享对象(每次活动被破坏时),但如果我的程序被破坏,我应该关闭它。那么:在哪里调用 .shutdown() ?或者有人知道更好的方法来确保 TTS 快速加载吗?
I'm program an Android-App for blind people. So I'm using TTS on every Activity in my App.
I don't want to initialize TTS on every activity again because it takes time and resources, so I'm creating a TTS-Object in my application class and use it on the activitys. It works fine.
But documentation says you have to something like this:
protected void onDestroy() {
super.onDestroy();
if (mTts != null) {
mTts.shutdown();
}
}
But I can't do this because i don't want to shutdown my shared object (each time a activity is destroyed) but if my programm get's distroyed it i should shutdown it. So: Where to call .shutdown() ? Or does anybody know a better way to ensure that TTS is loading quickly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我强烈建议在服务中使用 TTS,而不是在活动中。另外,请确保在拨打任何通话或合成文件之前等待 TTS 服务完成初始化。您可以对服务中的 TTS 对象进行静态单例引用,您可以在服务启动后从任何活动中获取该对象。如果需要的话,我可以稍后发布一些代码片段。
I highly recommend using TTS from within a Service, rather than an Activity. Also, make sure that you wait for the TTS service to finish init before making any calls to speak or synthesize files. You can make a static singleton reference to your TTS object in the service, that you can get from any activity after the service is started. I can post some code snippets later if needed.
我找到了一个适合我的解决方案:
全局 TTS 引擎仅在没有更多链接时才会关闭。因此,我在应用程序的主要活动中“使用”TTS,所有其他活动都与其相关。因此,任何时候都有参考,发动机不会关闭。
顺便说一句:如果你不执行 .shutdown() ,你会收到此错误:
I've found a solution that works for me:
The global TTS-engine does the shutdown only if there is no more link to it. So I'm "using" TTS in the mainactivity of my app and all other activitys are related to it. So there is at any time a reference and the engine doesn't shutdown.
btw: If you doen't make the .shutdown() you get this error:
仅供参考,TTS 不可在类之间继承。它已记录在 Android Google 论坛上。我看到你正在毁灭。您是否也关闭了Pause?
FYI, TTS is not inheritable between classes. Its been documented on the Android Google Forum. I see your on destroy. Are you also closing onPause?