问题描述
我写这充分说明用户的活动,我很想阻塞TextToSpeech的初始化 - 否则超时。我怎样才能让我的线程等待?
I am writing an activity that speaks to the user and I'd really like to block on TextToSpeech initialization - or else time out. How can I get my thread to wait?
我想:
while (! mIsTtsReady || i>limit) try { Thread.sleep(100); i++; ... };
以及
@Override
public void OnInit() { mIsTtsReady = true; } // TextToSpeech.OnInitListener
不过的OnInit()永远不会运行。似乎的OnInit(通过一个消息到我的活动活套?),这是在紧张的睡眠()循环我的线程中执行。
But OnInit() never runs. It seems that OnInit executes within my thread (via a message to my activities Looper?), which is in a tight sleep() loop.
这似乎是错误的把我的大部分code(以下简称初始化之后的东西)到是OnInit本身。其移入一个Runnable,然后开始()荷兰国际集团它,如上面睡了可运行的工作范围内。但现在我的code是在一个新的线程,需要明确的同步与UI等,而这一切就会变得混乱真的很快。
It seems wrong to put the bulk of my code (the "after init" stuff) into OnInit itself.Moving it into a Runnable, then start()ing it, and sleeping as above within that runnable works. But now my code is in a new thread and needs explicit syncing with the UI etc, and it all gets messy really quickly.
什么是正确的方式 - 或者至少是一个工程:) - 做到这一点
What is the right way - or at least one that works :) - to do this?
谢谢!
推荐答案
您需要初始化内如TTS系统该活动的onCreate()方法,这样就可以使用它以后的用户,例如,当点击一个按钮。
You need to initialize the TTS system within e.g. the activities onCreate() method, so that you can use it later when the user e.g. clicks a button.
请参阅例如https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/OneTweetActivity.java#L62其中,setupspeak()被调用,然后再说话()(https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/OneTweetActivity.java#L344 )其中,当用户点击发言按钮,然后调用
See e.g. https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/OneTweetActivity.java#L62 where setupspeak() is called and then later speak() ( https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/OneTweetActivity.java#L344 ) which is then called when the user clicks the 'speak' button.
这篇关于如何等待TextToSpeech的初始化在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!