如何阻止当前的活动,直到TTS引擎没有初始化 [英] how to block current activity till the TTS Engine is not initialized

查看:230
本文介绍了如何阻止当前的活动,直到TTS引擎没有初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了具有使用TTS引擎的应用程序。

i have developed an application which has to use TTS Engine.

现在的问题是,我需要等到TTS引擎被初始化,但我使用的睡眠方法时不尽如人意。

Now the problem is i need to wait till the TTS engine is initialized,though i am using sleep method it is not working well.

这是我的code

public void onCreate(Bundle savedInstanceState) {

    ......

    Intent checkin = new Intent();
    checkin.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkin, MY_DATA_CHECKCODE1);
    //l.setOnTouchListener(this);

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

     tts.speak("Enter sms", TextToSpeech.QUEUE_FLUSH, null);

}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {


if (requestCode == MY_DATA_CHECKCODE) {

if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {

// success, create the TTS instance

tts = new TextToSpeech(this, this);

tts.setLanguage(Locale.US);

tts.setSpeechRate((float) 0.4);


        } 
    }

}

代替上述的睡眠方法

我需要等到TTS引擎没有
initialized.so我怎么能写一个整洁块code,继续
后的当前活动 TTS引擎被初始化,而不是做一个硬编码,以检查在一个循环是否TTS
已被初始化。(我甚至不知道如何做到这一点为好)。

instead of above sleep method i need to wait till the TTS Engine is not
initialized.so how can i write a neat block of code to resume the current activity after
TTS Engine is initialized, instead of doing a hard coding to check in a loop whether TTS
has been initialized.(i even dont know how to do this as well).

该应用程序在运行非响应状态,并抛出空指针异常

The application is running in to Non-responding state and throwing Null pointer Exception

当我使用

tts.speak("Enter sms", TextToSpeech.QUEUE_FLUSH, null);

方法。所以,请建议我的解决方案来解决这个问题。我用Google搜索,但没有找到任何合适的答案。

method. So please suggest me the solution to fix this problem. I have googled but could not find any suitable answer.

推荐答案

请阅读本教程的彻底解决你的问题。

Please read this tutorial completely to solve your question.

- 一旦看了上面读<一href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/TextToSpeechActivity.html"相对=nofollow>这个例子同时,这将给你更深入的了解。

-Once read above read this example also which will give you more insight.

这篇关于如何阻止当前的活动,直到TTS引擎没有初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆