I am building an android native app for visually impaired people, And I want to use android TTS - android.speech.tts.TextToSpeech to guide the user to use my app. I succeeded in displaying the speech after a click on a button, but also I want to output a welcome message once the Activity is visible. here is a code snippet:
public class MainActivity extends AppCompatActivity {
private TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status==TextToSpeech.SUCCESS){
int result=textToSpeech.setLanguage(Locale.getDefault());
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language not supported");
} else {
Log.d("TTS","Speech initialized");
}
} else {
Log.e("TTS", "Initialization failed");
}
}
});
speak("welcome");
speak("Click on the button to begin settings ");
audio.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
speak("this is a test");
}
});
}
public void speak(final String S){
textToSpeech.speak(S,TextToSpeech.QUEUE_ADD,null);
}
}