'UtteranceProgressListener does not return error message

I have a piece of code to synthesize speech. There is currently an error and I want to find out which.

I have attached an UtteranceProgressListener to my TextToSpeech object. When textToSpeech.speak is called the function onError(String utteranceId) is called, but not the function onError(String utteranceId, int errorCode). I have set the APK-version to 22 and I use my Sony Xperia XZ2 Compact. The command textToSpeech.getEngines() returns com.google.android.tts

public class Text2Speech extends UtteranceProgressListener {
    private Context mContext;
    private TextToSpeech textToSpeech;
    private final String SPEECH_ID = "SPEECH";
    private boolean isReady = false;
    public Text2Speech(Context context, final String src){
        mContext = context;
        System.out.println("text2Speech created");
        textToSpeech = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    Log.e("Initialization: ", "Sucess");
                    isReady = true;
                    Locale locale = new Locale(src);
                    int ttsLang = textToSpeech.setLanguage(locale);
                    if (ttsLang == TextToSpeech.LANG_MISSING_DATA
                            || ttsLang == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "The Language is not supported!");
                    } else {
                        Log.i("TTS", "Language Supported.");
                    }
                    Log.i("TTS", "Initialization success.");
                } else {
                    Toast.makeText(mContext, "TTS Initialization failed!", Toast.LENGTH_SHORT).show();
                }
            }
        });
        textToSpeech.setOnUtteranceProgressListener(this); // <-- Assigning listener
    }

    public boolean isReady(){
        return isReady;
    }
    public void checkSpeaking(){
        if (textToSpeech.isSpeaking()){
            textToSpeech.stop();
        }
    }

    public void showMessage(String msg){
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }
    public void speakText(String text){
        textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, SPEECH_ID);
    }

    public void setLanguage(String src){
        Locale locale = new Locale(src);
        int tts = textToSpeech.setLanguage(locale);
        System.out.println(tts + "  " + src);
        if (tts == TextToSpeech.LANG_MISSING_DATA
                || tts == TextToSpeech.LANG_NOT_SUPPORTED) {
            Toast.makeText(mContext, "Language not yet supported.", Toast.LENGTH_LONG).show();
        }
    }

    public void stop(){
        textToSpeech.stop();
        textToSpeech.shutdown();
    }

    @Override
    public void onStart(String utteranceId) {
        Log.e("START", "start speaking");
    }

    @Override
    public void onDone(String utteranceId) {
        Log.e("DONE", "done speaking");
    }

    @Override
    public void onError(String utteranceID){
        Log.e("Error", utteranceID); // <-- This is called
    }
    @Override
    public void onError(String utteranceId, int errorCode) {
        Log.e("Error", "Error speaking"); <-- This is never called
        switch (errorCode){
            case TextToSpeech.ERROR_INVALID_REQUEST:
                showMessage("Invalid Request");
                break;
            case TextToSpeech.ERROR_NETWORK:
                showMessage("Network Error");
                break;
            case TextToSpeech.ERROR_NETWORK_TIMEOUT:
                showMessage("Network Timeout");
                break;
            case TextToSpeech.ERROR_NOT_INSTALLED_YET:
                showMessage("Error Not Yet Downloaded");
                break;
            case TextToSpeech.ERROR_OUTPUT:
                showMessage("Output Error");
                break;
            case TextToSpeech.ERROR_SERVICE:
                showMessage("Error of TTS service");
                break;
            case TextToSpeech.ERROR_SYNTHESIS:
                showMessage("Error synthesizing");
                break;
            case TextToSpeech.LANG_NOT_SUPPORTED:
                showMessage("Language nor supported");
                break;
        }
    }
}

Have anyone experience of this? This question is related to this question.



Solution 1:[1]

You have not used well the method TextToSpeech.speak.

If you want to add the callback UtteranceProgressListener you have to do this:

public void speakText(String text){
    textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID);
}

For more information about it, visit this web site TextToSpeech Engine

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ladytoky0