Android TextToSpeech doesn't work. Not bound to TTS engine

30 Views Asked by At

My TextToSpeech return error W/TextToSpeech: speak failed: not bound to TTS engine.

Changing targetSdkVersion to 28 doesn't help to fix this problem (now my current version is 32. I also have tried with 30).

Inside onInit method I see status = -1 (ERROR)

Help pls to fix this problem or may be get advice how I can perform speech function using some another implementations.

MainActivity.java

public class MainActivity extends Activity {
  
  private TextView myTextView;
  private MainActivityListeners listeners = new MainActivityListeners(this);

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.myTextView = findViewById(R.id.tv_myView);
   this.myTextView.setOnClickListener(this.listeners.getTranslateClickListener(this.myTextView));
}

MainActivityListeners.java

public class MainActivityListeners implements TextToSpeech.OnInitListener {
    
    private MainActivity activity;
    private TextToSpeech myTextToSpeech;
    private boolean mIsInit;

    public MainActivityListeners(MainActivity activity) {
        this.activity = activity;
}

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            Locale locale = new Locale("eng");
            int result = mTextToSpeech.setLanguage(locale);
            if (result == TextToSpeech.LANG_MISSING_DATA) {
                mIsInit = false;
            } else {
                mIsInit = true;
            }
        } else {
            mIsInit = false;
        }
    }

View.OnClickListener getTranslateClickListener(TextView myTextView) {
        return x -> {
                this.myTextToSpeech = new TextToSpeech(this.activity, this);
                this.myTextToSpeech.speak("Hello", TextToSpeech.QUEUE_FLUSH, null, "someId");
            }
        };
    }

}
1

There are 1 best solutions below

0
PineapplePie On

There should be a text-to-speech engine installed on your device. It's a separate app (hidden or not, depends on the manufacturer). It's usually pre-installed on most phones, but not all Android emulators have it (and especially custom skins). You can check in the system settings if you can find and check-box the "text-to-speech setting", if it's missing (like zero engines are available), you can download it from PlayStore (I'm usually taking this one).