Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android TextToSpeech初始化阻止/冻结UI线程_Android_Multithreading_User Interface_Blocking - Fatal编程技术网

Android TextToSpeech初始化阻止/冻结UI线程

Android TextToSpeech初始化阻止/冻结UI线程,android,multithreading,user-interface,blocking,Android,Multithreading,User Interface,Blocking,我编写了以下代码: public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } private TextToSpeech mTTS; @Override

我编写了以下代码:

public class MainActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
   }

   private TextToSpeech mTTS;

   @Override
   protected void onPause() {
       super.onPause();
       if (mTTS != null) {
           mTTS.stop();
           mTTS.shutdown();
       }
   }

   @Override
   protected void onResume() {
       super.onResume();
       mTTS = new TextToSpeech(getApplicationContext(),
               new TextToSpeech.OnInitListener() {
                   @Override
                   public void onInit(int status) {
                       if(status != TextToSpeech.ERROR){
                           mTTS.setLanguage(Locale.ENGLISH);
                           mTTS.speak("Hello!", TextToSpeech.QUEUE_FLUSH, null);
                       }
                   }
               });       
   }

   public void onButtonClick(View view) {
       mTTS.speak("Hello!", TextToSpeech.QUEUE_FLUSH, null);
   }
}
但是这个代码:mTTS=newtexttospeech(…冻结UI线程5-8秒)

我注意到延迟发生在logcat(第一行)中的这一行上:

我试图将其放入AsyncTask中:

@Override
protected void onResume() {
    super.onResume();

    MyAsyncTask newTask = new MyAsyncTask() {
        protected void onPostExecute(Boolean result) {
        }
    };
    newTask.context = getApplicationContext();
    newTask.execute();
}
...

class MyAsyncTask extends AsyncTask<Void, Integer, Boolean> {
    private TextToSpeech mTTS;
    public Context context;
    @Override
    protected Boolean doInBackground(Void... arg0) {
        mTTS = new TextToSpeech(context,
                new TextToSpeech.OnInitListener() {
                    @Override
                    public void onInit(int status) {
                        if(status != TextToSpeech.ERROR){
                            mTTS.setLanguage(Locale.ENGLISH);
                            mTTS.speak("Hello!", TextToSpeech.QUEUE_FLUSH, null);
                        }
                    }
                });
        return true;
    }
}
@覆盖
受保护的void onResume(){
super.onResume();
MyAsyncTask newTask=新建MyAsyncTask(){
受保护的void onPostExecute(布尔结果){
}
};
newTask.context=getApplicationContext();
newTask.execute();
}
...
类MyAsyncTask扩展了AsyncTask{
私有文本语音MTT;
公共语境;
@凌驾
受保护的布尔doInBackground(无效…arg0){
mTTS=新文本到语音(上下文,
新的TextToSpeech.OnInitListener(){
@凌驾
公共无效onInit(int状态){
if(状态!=TextToSpeech.ERROR){
mTTS.setLanguage(Locale.ENGLISH);
speak(“Hello!”,TextToSpeech.QUEUE\u FLUSH,null);
}
}
});
返回true;
}
}
但是没有什么变化。你能建议一个合适的解决方案/想法吗

这种延迟体现在我的手机LG L4 II(E440)上。在Nexus 10上–没有延迟。
我在我的LG L4上尝试了Play Store中的不同对话应用程序。在一些应用程序上也存在UI阻塞,但有些应用程序在没有阻塞的情况下工作。这意味着–可以实现。但是如何实现?

您不必在后台线程上进行演讲

IMO-您需要实现(TextToSpeech.OnInitListener、utranceProgressListener)


代码正常,不需要从其他线程调用TextToSpeech构造函数

实际上,问题在于一些TextToSpeech库的实现中,这些库使用主UI线程进行语音处理

解决方案是使TextToSpeech处理在一个独立的进程中运行,与UI线程的进程不同。这样,UI交互和语音处理在两个不同进程的主线程中完成。我发现唯一的方法是在Android服务中使用I创建TextToSpeech对象和控制代码它有自己的过程

为了使用自己的进程创建服务,AndroidManifest.xml中的配置必须包含属性:android:process

    <service
        android:name=".TtsService"
        android:enabled="true"
        android:exported="false"
        android:process="com.example.ttsservice">
    </service>


这会带来复杂性,因为现在活动必须使用Messenger或AIDL()绑定服务以管理TextToSpeech功能。

TextToSpeech.OnInitListener已经实现(第一个代码)。我刚刚实现了OuttanceProgressListener–未做任何更改。返回android示例…查看..../sdkRoot/samples/legacy/ttsEngine…将其引入现有应用程序,就像在示例/片段中显示的那样。我已经查看了此android示例。您建议实现服务吗?不,我不会在上的应用程序中使用该服务tts。谢谢你的回答和重要信息。由于这次延迟,我已经放弃了这个tts。稍后我会尝试你的服务解决方案(如果谷歌不解决它)。这个问题解决了吗?已经4年了。
                     tts.setOnUtteranceProgressListener(new UtteranceProgressListener()
                     {
                         @Override
                         public void onDone(String utteranceId)
                         {
                             onDoneSpeaking(utteranceId);
                         }

                         @Override
                         public void onError(String utteranceId)
                         {
                         }

                         @Override
                         public void onStart(String utteranceId)
                         {
                         }
                     });
...

    public void onInit(int status) {
        if ( status == TextToSpeech.SUCCESS ) {
            int result = tts.setLanguage( Locale.US );
            if ( result == TextToSpeech.LANG_MISSING_DATA ||
                    result == TextToSpeech.LANG_NOT_SUPPORTED ) {
                Log.e("MainActivity#onInit", "set lang error with "+result);
            }
            else {
                tts.speak(mcomment.substring(left, right), TextToSpeech.QUEUE_FLUSH, null);
    <service
        android:name=".TtsService"
        android:enabled="true"
        android:exported="false"
        android:process="com.example.ttsservice">
    </service>