BAD*_*DIA 6 java android text-to-speech google-text-to-speech android-fragments
我有我的课程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这是我初始化 MyTTS 类并调用 talkOut 方法的类:
\n\n片段.java:
\n\npublic class must_adapter_frag extends Fragment{\n @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)\n @SuppressLint("JavascriptInterface")\n @Override\n public View onCreateView(LayoutInflater inflater, ViewGroup container,\n Bundle savedInstanceState) {\n //Inflate the layout for this fragment\n\n MyTTS myTTS=new MyTTS(getActivity());\n\n View view= inflater.inflate(R.layout.webview_frag, container, false);\n System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");\n myTTS.speakOut("salam","dj");\n\n System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");\n WebView webView=view.findViewById(R.id.webview);\n webView.getSettings().setJavaScriptEnabled(true);\n webView.addJavascriptInterface(myTTS,"Android");\n webView.loadUrl("file:///android_asset/"+String.valueOf(getArguments().getInt("position"))+".html");\n\n\n\n return view;\n }\n\n @Override\n public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {\n super.onViewCreated(view, savedInstanceState);\n getActivity().setTitle("\xd8\xaa\xd8\xb9\xd9\x84\xd9\x8a\xd9\x82");\n\n }\nRun Code Online (Sandbox Code Playgroud)\n\n如果有专业人士可以帮我解决这个问题!
\n\n我对我的英语感到抱歉,因为这不是我的语言。
\n您收到该错误是因为 MyTTS 对象内的 TextToSpeech 对象(您从片段内部创建的)尚未初始化。
在 MyTTS 类中,您可以看到 onInit() 方法内有 talkOut() 。这就是它在那里正常工作的原因......因为 onInit 仅在 textToSpeech 对象初始化后调用。
所以...你可以做的是:
public boolean textToSpeechIsInitialized = false; // <--- add this line
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
textToSpeechIsInitialized = true; // <--- add this line
int result = textToSpeech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakOut("badr","dfd");
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
Run Code Online (Sandbox Code Playgroud)
然后在您的片段中,确保在调用 talkOut() 之前先检查该布尔标志:
System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
if (myTTS.textToSpeechIsInitialized) { // <--- add this line
myTTS.speakOut("salam","dj");
} else {
// try again later
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4103 次 |
| 最近记录: |