我有我的课程MyTTS,我有方法speakout在这门课上有方法。当我在类内部调用它时,它工作正常,但是如果我在其他类中初始化这个类并且我再次调用这个方法就永远不起作用,它给了我
\n\n\nW/TextToSpeech:说话失败:未绑定到 TTS 引擎
\n
这是我的类 MyTTS.java:
\n\nTextToSpeech textToSpeech;\n\n\npublic MyTTS(Context context) {\n textToSpeech=new TextToSpeech(context,this);\n}\n\n\n\n@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)\npublic void speakOut(String str,String pk){\n textToSpeech.speak(str,TextToSpeech.QUEUE_FLUSH,null,pk);\n}\n\n@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)\n@Override\npublic void onInit(int status) {\n if (status == TextToSpeech.SUCCESS) {\n\n int result = textToSpeech.setLanguage(Locale.US);\n\n if (result == TextToSpeech.LANG_MISSING_DATA\n || result == TextToSpeech.LANG_NOT_SUPPORTED) {\n Log.e("TTS", "This Language is not supported");\n } else {\n speakOut("badr","dfd");\n }\n\n } else {\n Log.e("TTS", "Initilization Failed!");\n }\n}\n\n@Override\npublic void onPause() {\n\n if(textToSpeech==null){\n textToSpeech.stop();\n textToSpeech.shutdown();\n\n }\n\n super.onPause();\n}\nRun Code Online (Sandbox Code Playgroud)\n\n … java android text-to-speech google-text-to-speech android-fragments