How to do a simple texttospeech

Viewed 28

I've been trying to do a simple texttospeech application by trying out sample codes from both youtube and tutorial sites and nothing seems to work for me. I keep getting the "W/TextToSpeech: speak failed: not bound to TTS engine"

Here is the current code I'm trying out

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
Button speakBtn;
EditText speakText;
TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    speakText = (EditText) findViewById(R.id.txtSpeak);
    speakBtn = (Button)findViewById(R.id.btnSpeech);
    textToSpeech = new TextToSpeech(this, this);
    speakBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            texttoSpeak();
        }
    });
}
@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("error", "This Language is not supported");
        } else {
            texttoSpeak();
        }
    } else {
        Log.e("error", "Failed to Initialize");
    }
}
@Override
public void onDestroy() {
    if (textToSpeech != null) {
        textToSpeech.stop();
        textToSpeech.shutdown();
    }
    super.onDestroy();
}
private void texttoSpeak() {
    String text = speakText.getText().toString();
    if ("".equals(text)) {
        text = "Please enter some text to speak.";
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
    }
    else {
        textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}}

Sorry I don't know much yet and would appreciate any help. Thanks in advance

0 Answers
Related