Android确认绑定到文本到语音引擎

Android确认绑定到文本到语音引擎,android,binding,text-to-speech,Android,Binding,Text To Speech,我可以成功地实现TextToSpeech.OnInitListener,并在调用onInit()-方法时接收TextToSpeech.SUCCESS的int值。如果我将代码放在调用speech的onInit()-方法中,TTS对象会成功地播放该语句,但会有几秒钟的明显延迟 如果我在onInit()-方法之外立即使用代码调用语音,我会在LogCat中收到一条警告消息,告诉我TTS对象未绑定到TTS引擎,并且听不到任何声音。但是,如果我等待5秒钟左右,并在onInit()方法之外使用代码,我不会收到

我可以成功地实现
TextToSpeech.OnInitListener
,并在调用
onInit()
-方法时接收
TextToSpeech.SUCCESS
的int值。如果我将代码放在调用speech的
onInit()
-方法中,TTS对象会成功地播放该语句,但会有几秒钟的明显延迟

如果我在
onInit()
-方法之外立即使用代码调用语音,我会在LogCat中收到一条警告消息,告诉我TTS对象未绑定到TTS引擎,并且听不到任何声音。但是,如果我等待5秒钟左右,并在
onInit()
方法之外使用代码,我不会收到绑定警告,并且会听到声音

1.为什么在返回代码为
TextToSpeech.SUCCESS
之后,
onInit()中没有抛出错误(我在这里调用speech时没有等待)

2.返回的
TextToSpeech.SUCCESS
代码似乎并不表示TTS对象已绑定到TTS引擎-是否有其他方法可用于检查成功绑定

 protected void onCreate(Bundle savedInstanceState) {
        voice = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status = TextToSpeech.SUCCESS) {
                    voice.setLanguage(Locale.UK);
                    voice.speak("I successfully talk here, after a delay.", TextToSpeech.QUEUE_FLUSH, null);                        
                }
            }
        });

        voice.speak("Apparently, I'm not bound to the TTS engine.  Maybe too soon to talk.", TextToSpeech.QUEUE_FLUSH, null);
LogCat显示了“voice”对象完成实例化和我尝试使用它之间的定义延迟

11-07 21:45:11.534: D/MyActiviy(12350): About to instantiate voice  
11-07 21:45:11.574: I/TextToSpeech(12350): Sucessfully bound to com.google.android.tts  
11-07 21:45:11.574: D/MyActiviy(12350): About to speak outside of onInit  
11-07 21:45:11.574: W/TextToSpeech(12350): speak failed: not bound to TTS engine  
11-07 21:45:11.574: D/MyActiviy(12350): Should have spoken  
11-07 21:45:11.694: I/Adreno-EGL(12350): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13  
11-07 21:45:11.734: D/OpenGLRenderer(12350): Enabling debug mode 0  
11-07 21:45:12.114: I/TextToSpeech(12350): Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}  
11-07 21:45:12.114: I/TextToSpeech(12350): Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}  
11-07 21:45:12.124: D/MyActiviy(12350): voice onInit called.  
11-07 21:45:12.124: D/MyActiviy(12350): voice onInit status returned SUCCESS 
11-07 21:45:11.534:D/myActivy(12350):即将实例化语音
11-07 21:45:11.574:I/TextToSpeech(12350):成功绑定到com.google.android.tts
11-07 21:45:11.574:D/MyActivy(12350):即将在onInit之外发言
11-07 21:45:11.574:W/TextToSpeech(12350):讲话失败:未绑定到TTS引擎
11-07 21:45:11.574:D/我的活动(12350):应该说
11-07 21:45:11.694:I/Adreno EGL(12350):EGL 1.4高通公司版本:I0404C4692AFB8623F95C43AEB6D5E13ED4B30DDB日期:2013年6月11日
11-07 21:45:11.734:D/OpenGLRenderer(12350):启用调试模式0
11-07 21:45:12.114:I/TextToSpeech(12350):连接到组件信息{com.google.android.tts/com.google.android.tts.service.googleletssservice}
11-07 21:45:12.114:I/TextToSpeech(12350):设置到ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}的连接
11-07 21:45:12.124:D/MyActivy(12350):有人打电话给我。
11-07 21:45:12.124:D/MyActivity(12350):语音onInit状态返回成功

onInit回调中的成功状态表示您已绑定到TTS引擎,可以进行通话。如果onInit回调还没有发生,为什么您希望第二个speak调用在这里工作?这就是问题所在。对象“voice”是在我尝试在onInit方法之外讲话之前,通过调用onInit的构造函数实例化的。请参阅我在代码下面添加的LogCat实体。第二行似乎表示绑定成功,第四行表示未绑定。如果对象需要时间才能成功完成设置过程,我如何判断它何时真正绑定到引擎并可以正常运行?它在收到回调后绑定。绑定过程是异步进行的,因此不能在从构造函数返回后立即调用。您必须等待初始化回调。