I have my class MyTTS
and i have method speakout
in this class. When i call it inner the class it work fine but if i initialize this class in other class and i call this method again never works, it gives me
W/TextToSpeech: speak failed: not bound to TTS engine
this my class MyTTS.java:
TextToSpeech textToSpeech;
public MyTTS(Context context) {
textToSpeech=new TextToSpeech(context,this);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void speakOut(String str,String pk){
textToSpeech.speak(str,TextToSpeech.QUEUE_FLUSH,null,pk);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
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!");
}
}
@Override
public void onPause() {
if(textToSpeech==null){
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onPause();
}
and this the class that i initialise MyTTS class and call speakOut methode:
fragment.java:
public class must_adapter_frag extends Fragment{
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@SuppressLint("JavascriptInterface")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Inflate the layout for this fragment
MyTTS myTTS=new MyTTS(getActivity());
View view= inflater.inflate(R.layout.webview_frag, container, false);
System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
myTTS.speakOut("salam","dj");
System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
WebView webView=view.findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(myTTS,"Android");
webView.loadUrl("file:///android_asset/"+String.valueOf(getArguments().getInt("position"))+".html");
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("تعليق");
}
if any expert person can help me for solve this question please!
And i'm sorry about my English language cause is not my language.