如何知道TextToSpeech何时讲完?

时间:2013-01-30 08:39:13

标签: android text-to-speech

我正在为Android应用程序使用Text to Speech。我很困惑找到一个在Text to Speech完成后调用的方法。我试着调用以下方法,但是没有调用完成的,请查看以下代码并建议我。

 tts= new TextToSpeech(MainActivity.this, new OnInitListener() {

         @Override
         public void onInit(int status) {

             tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {

                @Override
                public void onUtteranceCompleted(final String utteranceId) {
                            System.out.println("Completed");

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                        //UI changes
                        }
                    });
                }
            });

         }
        });

请建议我。我使用的是API Level 17。

<小时/> 修改

最低API级别为12;

我在控制台中收到以下错误:

01-30 09:19:14.149: E/ActivityThread(660): Service com.android.exchange.ExchangeService

    has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ce0ef0 that was originally bound here
01-30 09:19:14.149: E/ActivityThread(660): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ce0ef0 that was originally bound here
01-30 09:19:14.149: E/ActivityThread(660):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
01-30 09:19:14.149: E/ActivityThread(660):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-30 09:19:14.149: E/ActivityThread(660):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-30 09:19:14.149: E/ActivityThread(660):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-30 09:19:14.149: E/ActivityThread(660):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-30 09:19:14.149: E/ActivityThread(660):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-30 09:19:14.149: E/ActivityThread(660):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-30 09:19:14.149: E/ActivityThread(660):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
01-30 09:19:14.149: E/ActivityThread(660):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
01-30 09:19:14.149: E/ActivityThread(660):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
01-30 09:19:14.149: E/ActivityThread(660):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
01-30 09:19:14.149: E/ActivityThread(660):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
01-30 09:19:14.149: E/ActivityThread(660):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-30 09:19:14.149: E/ActivityThread(660):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-30 09:19:14.149: E/ActivityThread(660):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-30 09:19:14.149: E/ActivityThread(660):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-30 09:19:14.149: E/ActivityThread(660):  at java.lang.Thread.run(Thread.java:856)
01-30 09:19:14.169: E/StrictMode(660): null
01-30 09:19:14.169: E/StrictMode(660): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ce0ef0 that was originally bound here
01-30 09:19:14.169: E/StrictMode(660):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
01-30 09:19:14.169: E/StrictMode(660):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-30 09:19:14.169: E/StrictMode(660):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-30 09:19:14.169: E/StrictMode(660):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-30 09:19:14.169: E/StrictMode(660):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-30 09:19:14.169: E/StrictMode(660):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-30 09:19:14.169: E/StrictMode(660):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-30 09:19:14.169: E/StrictMode(660):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
01-30 09:19:14.169: E/StrictMode(660):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
01-30 09:19:14.169: E/StrictMode(660):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
01-30 09:19:14.169: E/StrictMode(660):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
01-30 09:19:14.169: E/StrictMode(660):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
01-30 09:19:14.169: E/StrictMode(660):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-30 09:19:14.169: E/StrictMode(660):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-30 09:19:14.169: E/StrictMode(660):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-30 09:19:14.169: E/StrictMode(660):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-30 09:19:14.169: E/StrictMode(660):  at java.lang.Thread.run(Thread.java:856)

5 个答案:

答案 0 :(得分:5)

问题不在onUtteranceComplete函数中,最有可能被调用,但您尝试将输出打印到System.out,请改用android.utils.Log("tag", "success"),看看会发生什么。

此外,您需要设置UtteranceID才能使onUtteranceComplete生效 在调用tts.speak方法之前。总结一下:

            TextToSpeech tts= new TextToSpeech(MainActivity.this, new OnInitListener() {

         @Override
         public void onInit(int status) {

             tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {

                @Override
                public void onUtteranceCompleted(final String utteranceId) {
                            System.out.println("Completed");

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                        //UI changes
                        }
                    });
                }
            });

         }
    });
    Map<String,String> ttsParams = new HashMap<String, String>();
    ttsParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
            MainActivity.this.getPackageName());

    mTts.speak(text, TextToSpeech.QUEUE_FLUSH, ttsParams);

您获得的例外与onUtteranceComplete方法无关,如果是因为tts引擎很可能是您在完成活动时没有请求销毁您的实例。

修复添加

@Override
onDestroy(){
super.onDestroy();
mTts.shutdown(); //mTts is your TextToSpeech Object
}

到您的活动类

答案 1 :(得分:1)

简便方法

g01.json.gz

答案 2 :(得分:0)

从API级别15开始,您必须使用setOnUtteranceProgressListener代替setOnUtteranceCompletedListener

检查official doc

答案 3 :(得分:0)

试试这个:

if(tts.myTTS.isSpeaking())

你得到一个布尔值。

答案 4 :(得分:0)

我找到了&#34;话语&#34;是合成文件。

使用 while(!utteranceDone){thread.sleep ...} 和以下代码发言:

                                brTTS = new BroadcastReceiver(){
                                public void onReceive(Context p1, Intent p2) {
                                    //Log.d(TAG,"TTS ACTION_TTS_QUEUE_PROCESSING_COMPLETED, p2.getAction()="+p2.getAction()+", ? "+TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED);
                                    if (p2.getAction().equals(TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED) && tts != null) {
                                        utteranceDone=true;
                                    }
                                }
                            };
                            registerReceiver(brTTS, new IntentFilter(TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED));