简体   繁体   中英

W/TextToSpeech: speak failed: not bound to TTS engine

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.

You're getting that error because the TextToSpeech object inside the MyTTS object (that you created from inside the fragment) is not initialized yet.

Inside the MyTTS class, you can see you have speakOut() inside the onInit() method. That's why it does work properly there... because onInit is only called after the textToSpeech object is initialized.

So... what you could do is something like:

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!");
    }
}

Then in your fragment, make sure you check that boolean flag first before calling speakOut():

System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
        if (myTTS.textToSpeechIsInitialized) {  // <--- add this line
             myTTS.speakOut("salam","dj");
        } else { 
             // try again later 
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM