How to wait for TextToSpeech initialization on And

2019-02-25 06:48发布

问题:

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?

I tried:

 while (! mIsTtsReady || i>limit) try { Thread.sleep(100); i++; ... };

along with:

 @Override
 public void OnInit() { mIsTtsReady = true; }   // TextToSpeech.OnInitListener

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.

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?

Thanks!

回答1:

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.

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.



回答2:

public void init(final Context context, final OnProgressStart onStart) {
    _mTts = new TextToSpeech(context, new OnInitListener() {
        // Implements TextToSpeech.OnInitListener.
        public void onInit(int status) {
            // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
            if (status == TextToSpeech.SUCCESS) {
                _isInitialized = true;
                Services.getTTSS().setLanguage();
                LogUtil.logInfo("TTS connected", this);
                if(onStart != null)
                    onStart.onStart();
            } else {
                // Initialization failed.
                Log.e(Constants.LOGTAG, this.getClass().getName()
                        + " Could not initialize TextToSpeech.");
            }
        }
    });

I also used the thread sleep, but it seems it doesn't work anymore and actually there is a better way handling this. Just pass a callback using simple interface like:

    init(context, new OnProgressStart() {               
            public void onStart(String... args) {
                startSpeak();                   
            }
        });