W/TextToSpeech:发言失败:未绑定到 TTS 引擎

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

我有我的 class MyTTS 并且我在这个 class 中有方法 speakout。当我在 class 内部调用它时它工作正常但是如果我在其他 class 中初始化这个 class 并且我再次调用这个方法永远不会工作,它给了我

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

这是我的 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();
}

这是我初始化 MyTTS class 并调用 speakOut 方法的 class:

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("تعليق");

    }

请哪位专家能帮我解决这个问题!

我很抱歉我的英语不是我的语言。

您收到该错误是因为 MyTTS 对象(您从片段内部创建的)中的 TextToSpeech 对象尚未初始化。

在 MyTTS class 中,您可以看到 onInit() 方法中有 speakOut()。这就是它在那里正常工作的原因...因为 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!");
    }
}

然后在您的片段中,确保在调用 speakOut() 之前先检查布尔标志:

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