【问题标题】:Android TextToSpeech: speak failed: not bound to TTS engineAndroid TextToSpeech:说话失败:未绑定到 TTS 引擎
【发布时间】:2016-12-22 13:40:01
【问题描述】:

我的 TextToSpeech 在第一次运行时运行良好,但在使用“返回”键关闭应用程序并重新打开后就无法运行。错误是TextToSpeech: speak failed: not bound to TTS enginestatus 中的onInitERROR

我有一个类来处理 TTS:

public class VoiceGenerator {
    public TextToSpeech tts;
    private Context context;
    private String TAG = "Voice Generator";

    private static VoiceGenerator instance;

    private VoiceGenerator(Context context){
        this.context = context;
    }

    public static VoiceGenerator getInstance(Context context){
        if (instance == null) {
            instance = new VoiceGenerator(context);
        }

        return instance;
    }

    public void initializeTTS() {
        if (tts == null) {
            Log.d(TAG, "initialize tts");
            tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if (status != TextToSpeech.ERROR) {
                        Log.d(TAG, "initialize tts success");
                        tts.setLanguage(...);                           
                    }      
                }
            });    
        }
    }

    public void speak(){
        tts.speak(...)
    }

    public void shutdown(){
        if(tts != null) {   
            tts.stop();
            tts.shutdown();
            tts=null;
            Log.d(TAG, "TTS Destroyed");
        }
    }

}

我在 onCreate 中得到 VoiceGenerator 的实例:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
    voiceGenerator = VoiceGenerator.getInstance(this);
}

在 onStart 中初始化 TTS:

@Override
protected void onStart(){
    super.onStart();
    voiceGenerator.initializeTTS();
}

并在 onDestroy 中将其关闭:

@Override
protected void onDestroy() {
super.onDestroy();
voiceGenerator.shutdown();    
}

关于我做错了什么有什么想法吗?

【问题讨论】:

    标签: android text-to-speech activity-lifecycle


    【解决方案1】:

    您需要在MainActivity 上实现TextToSpeech.OnInitListener。 这是一个很好的例子。

    代码:

    public class MainActivity extends ActionBarActivity  implements TextToSpeech.OnInitListener{
    
        private TextToSpeech engine;
        private EditText text;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            text = (EditText) findViewById(R.id.text);
            engine = new TextToSpeech(this, this);
        }
    
    
    
        public void speakText(View v) {
    
            String textContents = text.getText().toString();
            engine.speak(textContents, TextToSpeech.QUEUE_FLUSH, null, null);
    
        }
    
        @Override
        public void onInit(int i) {
    
    
            if (i == TextToSpeech.SUCCESS) {
                //Setting speech Language
                engine.setLanguage(Locale.ENGLISH);
                engine.setPitch(1);
            }
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      使用 onResume

      @Override
      protected void onResume() {
      super.onResume();
      myTTS = new TextToSpeech(this, this);
           }
      }
      

      【讨论】:

        【解决方案3】:

        简单的解决方案,不是所有设备都支持 TextToSpeach ,尝试不同的设备,一个

        【讨论】:

          【解决方案4】:

          onInit 完成后只能让引擎说话,所以在 onInit() 中执行以下操作:

             if (status == TextToSpeech.SUCCESS) {
               tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null);      
          
             }
          

          【讨论】:

          • 问题是状态是 ERROR 所以这没有帮助。
          猜你喜欢
          • 2012-07-31
          • 2014-02-25
          • 2016-05-18
          • 2021-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-18
          • 2023-03-06
          相关资源
          最近更新 更多